android - FCM android onmessagereceived 未调用

标签 android firebase push-notification firebase-cloud-messaging

我已经在应用程序中实现了 FCM。其他 firebase 功能运行良好,即上传到 firebase 存储和 firebase 实时消息传递。但是当我第一次向设备发送推送通知时,它显示已成功发送通知,但未调用消息接收。然后当我发送另一个推送通知时,它立即显示未注册。然后它总是没有注册。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("myLog", "From: " + remoteMessage.getFrom());
    Log.d("myLog", "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}

应用程序.gradle:

apply plugin: 'com.google.gms.google-services'

项目.gralde:

classpath 'com.google.gms:google-services:3.1.0'

list :

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

设备 token 已正确刷新,每当我清除数据并重新打开应用程序时,它会立即打印新的设备 token 。

最佳答案

onMessageReceived(RemoteMessage remoteMessage) 方法根据以下情况调用。

  • FCM 响应 带有通知数据 block :

{ "to": "设备 token 列表", “通知”:{ "body": "你的通知正文", "title": "您的通知标题" }, “数据”:{ "body": "你的数据通知正文", "title": "标题中通知的标题", "key_1": "key_1 的值", "image_url": "www.abc.com/xyz.jpeg", “key_2”:“key_2 的值” }

  1. 前台应用:

onMessageReceived(RemoteMessage remoteMessage) 调用,在通知栏中显示 LargeIcon 和 BigPicture。我们可以从通知数据 block 中读取内容

  1. 后台应用:

onMessageReceived(RemoteMessage remoteMessage) 未调用,系统托盘将接收消息并从 notification block 中读取正文和标题,并在通知栏中显示默认消息和标题。

  • FCM 响应只有数据 block :

在这种情况下,从 json 中删除 notofocation block

{ "to": "设备 token 列表", “数据”:{ "body": "你的数据通知正文", "title": "标题中通知的标题", "key_1": "key_1 的值", "image_url": "www.abc.com/xyz.jpeg", “key_2”:“key_2 的值” }

  1. 前台应用:

onMessageReceived(RemoteMessage remoteMessage) 调用,在通知栏中显示 LargeIcon 和 BigPicture。我们可以从通知数据 block 中读取内容

  1. 后台应用:

onMessageReceived(RemoteMessage remoteMessage) 被调用,系统托盘不会收到消息,因为 notification 键不在响应中。在通知栏中显示 LargeIcon 和 BigPicture

代码

 private void sendNotification(Bitmap bitmap,  String title, String 
    message, PendingIntent resultPendingIntent) {

    NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
    style.bigPicture(bitmap);

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

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = mContext.getString(R.string.default_notification_channel_id);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);

        notificationManager.createNotificationChannel(notificationChannel);
    }
    Bitmap iconLarge = BitmapFactory.decodeResource(mContext.getResources(),
            R.drawable.mdmlogo);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.mdmlogo)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentText(message)
            .setContentIntent(resultPendingIntent)
            .setStyle(style)
            .setLargeIcon(iconLarge)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX)
            .setChannelId(NOTIFICATION_CHANNEL_ID);


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


}

引用链接:

https://firebase.google.com/docs/cloud-messaging/android/receive

关于android - FCM android onmessagereceived 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971149/

相关文章:

swift - swift语言中cloud Firestore server timestamp的等效数据类型是什么?

Android - 在锁定屏幕上执行推送通知操作时提示用户输入设备密码

android - 当日志显示没有错误时,为什么我的推送通知没有出现在设备上?

android - React Native 中的推送通知

java - Android MapView 类未找到

java - 错误 :Execution failed for task ':app:packageAllDebugClassesForMultiDex' . >

android - 如何从 AutoCompleteTextView 获取文本?

java - Firebase 模型 POJO getter 在 FirebaseRecyclerAdapter 中返回 null

Android InputMethodService 按键滑动语言更改

java - 在横向 View 中获取旋转(倾斜)角度 - Android Java