我正在我的应用程序中实现 FCM(Firebase 消息传递服务).这里一切似乎都很好,除非当应用程序处于后台状态时,我无法提取预期的通知数据.
I am implementing FCM (Firebase messaging Service) in my application. Here all seems ok except when app is in background state i am not able to extract expected notification data.
基于概念:FCM 中有两种类型的消息:
Based on concepts: There are two types of messages in FCM:
display-messages:这些消息仅在您的应用处于前台时有效.
display-messages: These messages only work when your app is in foreground.
数据消息:即使您的应用在后台,这些消息也能正常工作当我们的应用处于后台时,Android 会将通知消息定向到系统托盘.
data-messages: Theses messages work even if your app is in background When our app is in the background, Android directs notification messages to the system tray.
为了处理 数据消息,您的通知应该有 click_action = "YOUR_ACTION" 字段.
for handling data-messages your notification should have click_action = "YOUR_ACTION" field.
我的信息会是这样的:
{
"data": {
"body": "here is body",
"title": "Title",
"click_action": "YOUR_ACTION"
},
"to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
}
Activity 会显示 manifest 文件如下所示的消息:
The Activity will display the message that manifest file will like this:
<activity
android:name=".NotificationActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Dialog"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="YOUR_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
点击通知后,它将重定向到我的 NotificationActivity
.在我的 onCreate
和 onNewIntent
方法中的 NotificationActivity
中,我使用这种方式提取消息:
After clicking on Notification it will redirect to my NotificationActivity
. In my NotificationActivity
in onCreate
and onNewIntent
method i am extracting message using this way:
Bundle bundle=getIntent().getExtras();
if(bundle!=null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("DATA_SENT", String.format("%s %s (%s)", key,
value.toString(), value.getClass().getName()));
}
}
不幸的是,在我的 NotificationActivity
中,我收到以下消息:
Unfortunately in my NotificationActivity
i am getting below message:
google.sent_time:1471631793774
google.sent_time: 1471631793774
发件人:50711789666
from: 50711789666
google.message_id 0:1471631793776823%098e508d098e508d
google.message_id 0:1471631793776823%098e508d098e508d
collapse_key:com.myapp.package_name
collapse_key: com.myapp.package_name
但是我的预期通知数据在哪里?
这是我的系统配置:
Android Studio 版本:2.1.3
Firebase 版本:com.google.firebase:firebase-auth:9.0.1
Firebase Version: com.google.firebase:firebase-auth:9.0.1
Google Play 服务版本:com.google.android.gms:play-services:9.2.1
Google Play service version : com.google.android.gms:play-services:9.2.1
以下是一些相关链接:
提前致谢.抱歉英语不好.
Thanks in advance. Sorry for bad English.
上面的android解决方案是正确的.实际上,通知消息是问题所在.它向我发送数据"对象而不是通知"对象.我的 TargetActivity
中缺少通知"对象没有使用 getIntent()
获得消息.发送通知"对象后,它解决了我的问题.
The upper side android solution is correct. Actually, the notification message was the the problem. It sending me "data" object but not "notification" object. Lacks of "notification" object in my TargetActivity
didn't getting message using getIntent()
. After sending "notification" object it resolved my problem.
正确的消息格式如下:
{
"data": {
"body": "here is body",
"title": "Title"
},
"notification": {
"body": "here is body",
"title": "Title",
"click_action": "YOUR_ACTION"
},
"to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
}
这里是关于 firebase 消息的更清晰的概念.我是从他们的支持团队那里找到的.Firebase 具有三种消息类型:
Here is more clear concepts about firebase message. I found it from their support team. Firebase has three message types:
通知消息:通知消息在后台或前台工作.当应用程序处于后台时,通知消息将传递到系统托盘.如果应用程序在前台,消息由 onMessageReceived() 或 didReceiveRemoteNotification 回调处理.这些本质上就是所谓的显示消息.
Notification messages : Notification message works on background or foreground. When app is in background, Notification messages are delivered to the system tray. If the app is in the foreground, messages are handled by onMessageReceived() or didReceiveRemoteNotification callbacks. These are essentially what is referred to as Display messages.
数据消息:在Android平台上,数据消息可以在后台和前台工作.数据消息将由 onMessageReceived() 处理.这里有一个特定于平台的说明:在 Android 上,可以在用于启动您的活动的 Intent 中检索数据有效负载.详细地说,如果您有click_action":launch_Activity_1",则可以通过 getIntent() 仅从 Activity_1 检索此意图.
Data messages: On Android platform, data message can work on background and foreground. The data message will be handled by onMessageReceived(). A platform specific note here would be: On Android, the data payload can be retrieved in the Intent used to launch your activity. To elaborate, if you have "click_action":"launch_Activity_1", you can retrieve this intent through getIntent() from only Activity_1.
同时包含通知和数据负载的消息:在后台时,应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载.在前台时,您的应用会收到一个消息对象,其中包含两个可用的有效负载.其次,click_action 参数通常用于通知负载而不是数据负载.如果在数据负载中使用,此参数将被视为自定义键值对,因此您需要实现自定义逻辑以使其按预期工作.
Messages with both notification and data payloads: When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification. When in the foreground, your app receives a message object with both payloads available. Secondly, the click_action parameter is often used in notification payload and not in data payload. If used inside data payload, this parameter would be treated as custom key-value pair and therefore you would need to implement custom logic for it to work as intended.
另外,我建议您使用 onMessageReceived
方法(请参阅数据消息)来提取数据包.根据您的逻辑,我检查了 bundle 对象并没有找到预期的数据内容.这是对类似案例的参考,可能会提供更清晰的信息.
Also, I recommend you to use onMessageReceived
method (see Data message) to extract the data bundle. From your logic, I checked the bundle object and haven't found expected data content. Here is a reference to a similar case which might provide more clarity.
这篇关于当应用程序处于后台状态时,Google FCM getIntent 未返回预期数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!