首页 > 解决方案 > 在 android 版本 8+ 中未收到 FCM 通知,也未调用 onMessageReceived()

问题描述

这是我的代码

@Override
 public void onMessageReceived(RemoteMessage remoteMessage) {


  Intent intent = new Intent(this, SOMEACTIVITY.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(Notification.DEFAULT_ALL);


        notificationBuilder.setSmallIcon(R.drawable.ic_notification_white_icon);

        if (image != null) {
            notificationBuilder
                    .setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(image).setSummaryText(messageBody));/*Notification with Image*/
        } else {
            notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));
        }

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


        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel Name",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0, notificationBuilder.build());

 }

任何人都可以帮助我对
firebase的依赖:实现'com.google.firebase:firebase-messaging:12.0.1'

buidgradle main : 类路径 'com.google.gms:google-services:4.0.1'

我已经尝试过FCM 通知没有在 Oreo 的后台被调用

标签: androidfirebasefirebase-cloud-messaging

解决方案


推荐阅读