Firebase扩展通知在应用程序处于后台时显示图像

Firebase扩展通知在应用程序处于后台时显示图像

问题描述:

我正在Android中实现FCM通知,但是通知根据应用程序状态(后台还是前台)有何不同?

I am implementing FCM notifications in Android, but how does notifications differ depending on the app status (background vs. foreground)?

我正在使用带有Postman的FCM API发送通知,这是通知结构:

I am sending the notification using the FCM API with Postman and this is the notification structure:

{ "notification": {
      "title": "Notification title",
      "body": "Notification message",
      "sound": "default",
      "color": "#53c4bc",
      "click_action": "MY_BOOK",
      "icon": "ic_launcher"
   },
   "data": {
       "main_picture": "URL_OF_THE_IMAGE"  
   },
   "to" : "USER_FCM_TOKEN"
}

要渲染的图像是从data.main_picture拍摄的.

The image to render is taken from data.main_picture.

我已经实现了自己的FirebaseMessagingService,它使通知在前台状态下完美显示.通知代码是下一个:

I have implemented my own FirebaseMessagingService which makes the notifications display perfectly in foreground state. The notification code is the next:

NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(messageBody);
notiStyle.bigPicture(picture);

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

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(bigIcon)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setStyle(notiStyle); code here

NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

但是,在后台,该服务甚至没有执行.在AndroidManifest.xml中,Firebase服务声明如下:

However, in background, the service is not even executed. In the AndroidManifest.xml, Firebase services are declared as follow:

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

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

我的问题不是LargeIconSmallIcon,而是显示大图.

My problem is not the LargeIcon or SmallIcon but displaying the big picture.

感谢您的支持.

FCM notification messages不支持largeIcon或bigPicture.

FCM notification messages don't support the largeIcon or bigPicture.

如果您在后台需要它们,则可以使用FCM data message.

if you need them while in background you can use a FCM data message.

对于数据消息,总是调用onMessageReceived(message)方法,因此您可以使用message.getData()方法并创建自定义通知.

For data messages the onMessageReceived(message) method is always called, so you can use the message.getData() method and create your custom notification.

在此处阅读有关通知消息和数据消息的更多信息: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

Read more about notification messages vs data messages here: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages