当应用处于后台状态时,Google FCM getIntent不会返回预期的数据 [英] Google FCM getIntent not returning expected data when app is in background state

查看:730
本文介绍了当应用处于后台状态时,Google FCM getIntent不会返回预期的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中实施FCM(Firebase消息服务)。这里一切似乎好,除非应用程序处于后台状态,我不能提取预期的通知数据。

基于概念:在FCM中有两种类型的消息: / p>

显示讯息:只有当您的应用处于前台时,这些讯息才会有效。

数据消息:即使您的应用程序处于后台,这些消息也能正常工作
当我们的应用程序位于后台时,Android会将通知消息发送到系统托盘。 b

用于处理数据消息您的通知应具有 click_action =YOUR_ACTION字段。


$ b

  {
data:{
body :this is body,
title:Title,
click_action:YOUR_ACTION
},
to:ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb

活动将显示清单文件将l ike this:

 < activity 
android:name =。NotificationActivity
android:screenOrientation = portrait
android:theme =@ style / Theme.AppCompat.Dialog
android:windowSoftInputMode =stateHidden>

< intent-filter>
< category android:name =android.intent.category.DEFAULT/>
< / intent-filter>
< / activity>

点击通知后,它会重定向到我的 NotificationActivity 。在我的 NotificationActivity onCreate onNewIntent 消息使用这种方式:

  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())) ;






$ b

不幸的是我的 NotificationActivity 我收到以下消息:



google.sent_time :1471631793774

:50711789666

$ b google.message_id 0:1471631793776823%098e508d098e508d


$ b collapse_key :com.myapp.package_name



但是,我的预期通知数据在哪里?

以下是我的系统配置:

Android Studio版本: 2.1.3

Firebase版本:com.google.firebase:firebase-auth:9.0.1



Google Play服务版本:com.google.android.gms:play-services:9.2.1



一些相关的链接:



在此先感谢您。对不起,英文不好。

解决方案

上面的android解决方案是正确的。实际上,通知消息是问题所在。它向我发送数据对象而不是通知对象。我的 TargetActivity 中缺少通知对象,没有使用 getIntent()获取消息。正确的信息格式如下:

  {
data:{
body:here is body,
title:Title
},
通知:{
body:here is body,
title:Title,
click_action:YOUR_ACTION
},
to:ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb

这里是关于firebase消息的更清晰的概念。我从他们的支持团队找到了它。
Firebase具有三种消息类型


通知消息:通知消息在后台或前台运行。当应用程序在后台时,通知消息被传递到系统托盘。如果应用程序处于前台,则消息由onMessageReceived()或didReceiveRemoteNotification回调处理。这些实质上就是所谓的显示消息。数据消息:在Android平台上,数据消息可以在后台和前台运行。数据消息将由onMessageReceived()处理。
这里的平台特定的注意事项是:在Android上,数据有效载荷可以在用于启动您的活动的Intent中检索。详细说明一下,如果你有click_action:launch_Activity_1,你可以通过只有Activity_1的getIntent()来获取这个intent。
$ b 包含通知和数据有效负载的消息
当在后台时,应用程序会在通知托盘中收到通知负载,只有当用户点击通知时才处理数据有效载荷。
当在前台时,您的应用程序将收到一个消息对象,其中包含两个有效负载。
其次,click_action参数通常用于通知负载,而不是数据负载。如果在数据有效载荷中使用,则此参数将被视为自定义键值对,因此您需要实现自定义逻辑才能按预期工作。另外,我建议您使用 onMessageReceived 方法(请参阅数据消息)来提取数据包。从你的逻辑,我检查了捆绑对象,并没有找到预期的数据内容。这里提供了一个类似的案例,可能会提供更多的清晰。


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.

Based on concepts: There are two types of messages in FCM:

display-messages: These messages only work when your app is in foreground.

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.

for handling data-messages your notification should have click_action = "YOUR_ACTION" field.

My message will like this:

{
 "data": {
  "body": "here is body",
  "title": "Title",
  "click_action": "YOUR_ACTION"
 },
 "to": "ffEseX6vwcM:APA91bF8m7wOF MY FCM ID 07j1aPUb"
}

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>

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()));
            }
        }

Unfortunately in my NotificationActivity i am getting below message:

google.sent_time: 1471631793774

from: 50711789666

google.message_id 0:1471631793776823%098e508d098e508d

collapse_key: com.myapp.package_name

But where is my expected notification data?

Here is my system configuration:

Android Studio Version: 2.1.3

Firebase Version: com.google.firebase:firebase-auth:9.0.1

Google Play service version : com.google.android.gms:play-services:9.2.1

Here are some related links:

Thanks in advance. Sorry for bad English.

解决方案

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.

Correct message format is given bellow:

{
 "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"
}

Here is more clear concepts about firebase message. I found it from their support team. Firebase has three message types:

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.

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.

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.

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不会返回预期的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆