我已经在应用程序中实现了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());
}
}


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>


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

最佳答案

基于以下情况调用onMessageReceived(RemoteMessage remoteMessage)方法。


带通知和数据块的FCM响应:


{

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


前景中的应用程序:


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


后台应用程序:


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


FCM响应仅包含数据块:


在这种情况下,请从json移除notofocation块

{

“ to”:“设备令牌列表”,
“数据”:{
“ body”:“您的数据通知主体”,
“ title”:“标题中的通知标题”,
“ key_1”:“ key_1的值”,
“ image_url”:“ www.abc.com/xyz.jpeg”,
“ key_2”:“ key_2的值”
}
}


前景中的应用程序:


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


后台应用程序:


调用onMessageReceived(RemoteMessage remoteMessage)时,系统托盘将不会收到消息,因为通知键不在响应中。在通知栏中显示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

09-15 19:36