android – Firebase展开式通知当应用程序在后台时显示图像

前端之家收集整理的这篇文章主要介绍了android – Firebase展开式通知当应用程序在后台时显示图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在 Android中实施FCM通知,但根据应用状态(背景与前景)通知有何不同?

我正在使用FCM API与邮递员发送通知,这是通知结构:

  1. { "notification": {
  2. "title": "Notification title","body": "Notification message","sound": "default","color": "#53c4bc","click_action": "MY_BOOK","icon": "ic_launcher"
  3. },"data": {
  4. "main_picture": "URL_OF_THE_IMAGE"
  5. },"to" : "USER_FCM_TOKEN"
  6. }

要渲染的图像取自data.main_picture.

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

  1. NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
  2. notiStyle.setSummaryText(messageBody);
  3. notiStyle.bigPicture(picture);
  4.  
  5. Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  6.  
  7. NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
  8. .setSmallIcon(R.drawable.ic_launcher)
  9. .setLargeIcon(bigIcon)
  10. .setContentTitle(title)
  11. .setContentText(messageBody)
  12. .setAutoCancel(true)
  13. .setSound(defaultSoundUri)
  14. .setContentIntent(pendingIntent)
  15. .setStyle(notiStyle); code here
  16.  
  17. NotificationManager notificationManager =
  18. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  19. notificationManager.notify(0,notificationBuilder.build());

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

  1. <service
  2. android:name=".MyFirebaseMessagingService">
  3. <intent-filter>
  4. <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  5. </intent-filter>
  6. </service>
  7.  
  8. <service
  9. android:name=".MyFirebaseInstanceIDService">
  10. <intent-filter>
  11. <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
  12. </intent-filter>
  13. </service>

我的问题不是LargeIcon或SmallIcon,而是显示大图.

感谢您的支持.

解决方法

FCM通知消息不支持largeIcon或bigPicture.

如果您在后台需要它们,可以使用FCM数据消息.

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

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

猜你在找的Android相关文章