繁体   English   中英

未收到GCM推送通知

[英]GCM Push Notification not received

我尝试将此代码用于GCM推送通知。 在这个发送通知中,我使用this 但是它显示的消息很酷! 消息发送成功,正在检查您的设备...但是我的设备未收到通知。

尝试使用Firebase,这里是Push-Notification https://firebase.google.com/docs/cloud-messaging/的文档,或者您可以查看本教程https://www.simplifiedcoding.net/android-push-notification-tutorial -使用-火力/

您应该使用FCM。 看到

1)将此行添加到build.gradle:
dependencies { compile 'com.google.firebase:firebase-messaging:9.8.0'}

2)将此服务添加到清单。
<service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
3)将此类添加到您的项目中。 我建议您保存您的FCM令牌:

public class MyInstanceIDListenerService extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d("FirebaseService", "Refreshed token: " + refreshedToken);

    SharedPreferences sharedPref = getSharedPreferences("YOUR_SETTING_NAME", Context.MODE_PRIVATE);
    //There are optional steps
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("token", refreshedToken);
    //notify the token update 
    editor.putBoolean("tokenUpdate",true);
    editor.commit();
    //You can send this token to your server (if you have)
    sendServer(refreshedToken)
    }
}

4)现在,将您的应用程序注册到https://console.firebase.google.com/ 这将生成一个JSON文件,您必须将其放入应用程序文件夹中。

5)对于生成FCM消息,您有2种可能性:
5.1)使用默认的Firebase控制台: https ://console.firebase.google.com/project/fantamanager-2017/notification/compose
5.2)构建您的服务器应用程序(如果需要,我可以将您的Php服务器代码发送给您)

6)将此服务添加到您的Manifest.xml

<service android:name="FcmBroadcastReceiver">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
</service>

并创建一个可以拦截推送的类:

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final ID_NOTIFICATION = ...
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

       String from,data;
       from=remoteMessage.getFrom());


        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
           data=remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        sendNotification(data);
    }



    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(ID_NOTIFICATION, notificationBuilder.build());
    }
}

或者,如果您有自定义服务,请使用以下方法更改onMessageReceived函数的主体:

   Map data = message.getData();
    if (data.containsKey(YOUR_DATA_FIELD_1)) {
        String field1= data.get(YOUR_DATA_FIELD_1).toString();
        String field2= data.get(YOUR_DATA_FIELD_2).toString();
        ....
        sendNotification(field1,field2...);
        return;
    }

您是否尝试了部分或全部代码? 因为我最后看到Coz,因此gcm的新应用程序已关闭。 仅FCM可用。 因此,您需要按照他们所说的进行注册,在应用中实现json文件,然后继续。 如果您已经有一个实现gcm的项目,并且尝试了此代码的一部分,请检查密钥是否正确,并且尚未从该项目或其他项目复制它们的密钥。 或清单文件中的某些元数据丢失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM