Firebase 可扩展通知在应用程序处于后台时显示图像 [英] Firebase Expandable Notification Show image when app is in background
问题描述
我正在 Android 中实施 FCM 通知,但通知如何因应用状态(后台与前台)而异?
I am implementing FCM notifications in Android, but how does notifications differ depending on the app status (background vs. foreground)?
我使用 FCM API 和 Postman 发送通知,这是通知结构:
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
.
我已经实现了我自己的 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>
我的问题不是 LargeIcon
或 SmallIcon
而是显示大图.
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
这篇关于Firebase 可扩展通知在应用程序处于后台时显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!