首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Firebase消息,Android应用后台点击消息后未拦截推送通知

Firebase消息,Android应用后台点击消息后未拦截推送通知
EN

Stack Overflow用户
提问于 2018-10-20 17:48:21
回答 1查看 463关注 0票数 0

我使用firebase消息来发送推送通知。我的android应用程序有一个小问题。在iOS版本中,当应用程序在后台时,我可以很容易地在点击通知后传递参数。

在安卓版本中,我可以在应用程序处于前台时截取消息,但不能在应用程序处于后台时截取消息(我使用的是库com.google.firebase:firebase-messaging:17.3.4):

我的AndroidManifest.xml

代码语言:javascript
复制
<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>

我的通知负载:

代码语言:javascript
复制
notification:{
 title: "mytitle",
 text: "MYTEXT",
 "data":{
   "type" : "message",
   "id_message" : res
  },
  "click_action" : "OPEN_NOTIFY"
},to: token

当我单击通知时,它应该会打开MainActivity,并且我想让id_message

我的MyFirebaseMessagingService.java

代码语言:javascript
复制
 @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:

代码语言:javascript
复制
@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);
        }
    }
   ....
}

我无法截取我的通知:

输出:

代码语言:javascript
复制
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: ....

我走的是正确的道路吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-05-27 01:53:31

根据official Firebase docs的说法,你不能处理通知的{}部分通知(你只能在清单中设置它的图标),但这里有一个解决方案:通过数据{}部分发送必要的数据。你现在可以用MyFirebaseService来处理它,如果你想的话还可以显示通知:数据将出现在remoteMessage.toIntent().getExtras()中。在您的情况下,发送如下通知:

代码语言:javascript
复制
// ignore notification {}
data {
    "type" : "message",
    "id_message" : res
}
to: key

(不要将数据放入通知块)如果您愿意,我可以编写发送和接收通知的工作代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52904371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档