我使用firebase消息来发送推送通知。我的android应用程序有一个小问题。在iOS版本中,当应用程序在后台时,我可以很容易地在点击通知后传递参数。
在安卓版本中,我可以在应用程序处于前台时截取消息,但不能在应用程序处于后台时截取消息(我使用的是库com.google.firebase:firebase-messaging:17.3.4):
我的AndroidManifest.xml
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="OPEN_NOTIFY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
....
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
我的通知负载:
notification:{
title: "mytitle",
text: "MYTEXT",
"data":{
"type" : "message",
"id_message" : res
},
"click_action" : "OPEN_NOTIFY"
},to: token
当我单击通知时,它应该会打开MainActivity,并且我想让id_message
我的MyFirebaseMessagingService.java
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
//never enters if the app is in background
if(remoteMessage.getNotification() != null){
//I READ NOTIFY WHEN APP IS IN FOREGROUND
}
}
我尝试直接从Inten中获取参数,但此解决方案不起作用
我的MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
....
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
....
}
我无法截取我的通知:
输出:
Key: google.sent_time Value: 1540026384768
Key: google.ttl Value: 2419200
Key: from Value: 635549396240
Key: google.message_id Value: 0:1540026384773623%6b8fed3d6b8fed3d
Key: collapse_key Value: ....
我走的是正确的道路吗?谢谢
发布于 2019-05-27 01:53:31
根据official Firebase docs的说法,你不能处理通知的{}部分通知(你只能在清单中设置它的图标),但这里有一个解决方案:通过数据{}部分发送必要的数据。你现在可以用MyFirebaseService来处理它,如果你想的话还可以显示通知:数据将出现在remoteMessage.toIntent().getExtras()
中。在您的情况下,发送如下通知:
// ignore notification {}
data {
"type" : "message",
"id_message" : res
}
to: key
(不要将数据放入通知块)如果您愿意,我可以编写发送和接收通知的工作代码
https://stackoverflow.com/questions/52904371
复制相似问题