简体   繁体   English

未在设备上获取令牌和 FCM 通知

[英]not get token and FCM notification on device

I am integrate FCM in my app.Sent fcm from Firebase but not display on device.I am trying notification display when my app foreground and background.我在我的应用程序中集成了 FCM。从 Firebase 发送 fcm 但没有显示在设备上。我正在尝试在我的应用程序前台和后台时显示通知。

1-:connect app to Firebase and add FCM to my app.(Android Studio-->Tools-->Firebase-->Cloud Messaging) 1-:将应用程序连接到 Firebase 并将 FCM 添加到我的应用程序中。(Android Studio-->Tools-->Firebase-->Cloud Messaging)

2-:Create MyFirebaseMessageingService1 Class and extends by FirebaseMessagingService and implement Method (onNewToken and onMessageReceived) 2-:创建 MyFirebaseMessageingService1 类并通过 FirebaseMessagingService 扩展并实现方法(onNewToken 和 onMessageReceived)

3-:create method genrateNotification in onMessageReceived 3-:在onMessageReceived中创建方法genrateNotification

4-:add this service in manifest file and add action com.google.firebase.MESSAGING_EVENT 4-:在清单文件中添加此服务并添加操作 com.google.firebase.MESSAGING_EVENT

 private void genrateNotification(String body, String title) {
     Intent intent = new Intent(this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
         Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                 .setSmallIcon(R.drawable.app_icon)
                 .setContentTitle(title)
                 .setContentText(body)
                 .setAutoCancel(true)
                 .setSound(soundUri)
                 .setContentIntent(pendingIntent);

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

         if(NOTIFICATION_ID > 1073741824){
             NOTIFICATION_ID =0;
                     }
                     notificationManager.notify(NOTIFICATION_ID++,notificationBuilder.build());
     }

not get token and notification after implement above code执行上述代码后未获得令牌和通知

Before retrieving token make sure:在检索令牌之前,请确保:

  • Your device is connected with Internet.您的设备已连接互联网。
  • App is connected to firebase successfully.应用程序已成功连接到firebase
  • You have latest GooglePlayServices installed on device.您在设备上安装了最新的GooglePlayServices

Now ,to retrieve token any time you need:现在,随时检索令牌:

FirebaseInstanceId.getInstance().getInstanceId()
    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
        @Override
        public void onComplete(@NonNull Task<InstanceIdResult> task) {
            if (!task.isSuccessful()) {
                Log.w(TAG, "getInstanceId failed", task.getException());
                return;
            }

            // Get new Instance ID token
            String token = task.getResult().getToken();

            // Log and toast
            String msg = getString(R.string.msg_token_fmt, token);
            Log.d(TAG, msg);
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

Your MyNotificationService class:您的MyNotificationService类:

    public class MyNotificationService extends FirebaseMessagingService {

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);

            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            String channelId = "Default";

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(remoteMessage.getNotification().getTitle())
                    .setContentText(remoteMessage.getNotification().getBody())
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);


            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
                manager.createNotificationChannel(channel);
            }
            manager.notify(0, builder.build());
        }

        @Override
        public void onNewToken(String s) {
            super.onNewToken(s);
            Log.d(TAG, "NewToken: " + s);
        }
}

In your manifest:在您的清单中:

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

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

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