FCM android onmessagereceived not called



我在应用程序中实现了FCM。其他火池功能正常,即上传到Firebase存储和Firebase实时消息传递。但是,当我第一次将推送通知发送到设备时,它显示了成功发送的通知,但在messageceedived中未调用。然后,当我发送另一份推送通知时,立即显示未注册。然后它总是没有注册。

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());
}
}

app.gradle:

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

project.gralde:

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

清单:

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

设备令牌可以正确刷新,每当我清除数据并重新打开应用程序时,都会立即打印新的设备令牌。

onMessageCeived(remotemessage remototemessage)基于以下情况为调用的方法。

  • fcm响应带有 Notification data block:

{" to ":"设备令牌列表"," Notification ":{"身体":"通知的身体","标题":"通知标题"},," 数据":{"身体":"数据中的通知正文","标题":"标题中的通知标题"," key_1":" key_1的值"," image_url":" www.abc.com/xyz.jpeg"," key_2":" key_2的值"}}

  1. App在前景中:

onMessageReceived(remotemessage remototemessage)在通知栏中显示gindericon和bigpicture。我们可以从 Notification 数据> data block

中读取内容
  1. APP在后台:

onMessageReceived(remotemessage remototemessage)未调用,系统托盘将接收消息并从 Notification> Notification block中读取字体和标题,并在通知栏中显示默认消息和标题。

  • FCM响应仅使用 data 块:

在这种情况下,删除 notofocation json

{" to ":"设备令牌列表"," 数据":{"身体":"数据中的通知正文","标题":"标题中的通知标题"," key_1":" key_1的值"," image_url":" www.abc.com/xyz.jpeg"," key_2":" key_2的值"}}

  1. App在前景中:

onMessageReceived(remotemessage remototemessage)在通知栏中显示gindericon和bigpicture。我们可以从 Notification 数据> data block

中读取内容
  1. APP在后台:

onMessageReceived(remotemessage remotemessage)呼叫,系统托盘不会因为 notification 键而不会接收消息。在通知栏中显示大型和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

您正在使用哪种设备进行测试?众所周知,一些小米和Leeco这样的制造商可以防止应用程序接收FCM通知。

如果有三星,摩托罗拉(Motorola)等设备:确保您正在处理正确的消息类型。您可以发送2种类型的FCM消息。

  1. 通知消息
  2. 数据消息

阅读此页面以获取更多信息。

https://firebase.google.com/docs/cloud-messaging/concept-options

它取决于来自控制台的哪种类型消息类型。如果仅其通知消息,则您的应用程序已关闭,则您的接收器将不会触发。如果它带有通知消息数据消息,那么您的应用仍可以在应用程序在Foreground

时处理此消息。

确保您正在使用此依赖项compile 'com.google.firebase:firebase-messaging:10.2.1'

相关内容

  • 没有找到相关文章

最新更新