简体   繁体   English

当应用程序处于后台时,FCM Intent 与数据交付在哪里?

[英]Where is FCM Intent with data delivered, when app is in background?

I've implemented Firebase in my app and I'm sending push with extra data.我已经在我的应用程序中实施了 Firebase,并且正在发送带有额外数据的推送。 When my app is in foreground I'm handling data properly and showing my own notification, but I have a problem with fetching data, when Firebase shows Notification "automatically" when app was "homed" (not killed).当我的应用程序处于前台时,我正在正确处理数据并显示我自己的通知,但是当 Firebase 在应用程序“归巢”(未终止)时“自动”显示通知时,我在获取数据时遇到了问题。 According DOCS Activity should get new Intent with extras fulfilled with my values, instead app just get back to front, old state is restored.根据DOCSActivity应该获得新的Intent ,并用我的价值观实现额外的功能,而不是应用程序回到前面,恢复旧状态。

Scenario:设想:

  1. opening app, pressing Home打开应用程序,按主页
  2. sending push through Firebase console, Firebase is creating Notification WITHOUT calling onMessageReceived (according to table in docs, it should?)通过 Firebase 控制台发送推送,Firebase 正在创建Notification而不调用onMessageReceived (根据文档中的表,它应该?)
  3. when user pick notification app will be brought to front in same state as was "homed", and Intent is fulfilled with "original" extras used for open Activity on top当用户选择通知应用程序将以与“主页”相同的状态被带到前面时,并且Intent被用于在顶部打开Activity的“原始”附加功能实现

I have logs in onCreate , onNewIntent , onResume ( Activity ) and in onMessageReceived ( Service ), only onResume is called, in which I'm printing extras like below:我在onCreateonNewIntentonResume ( Activity ) 和onMessageReceived ( Service ) 中登录,只调用onResume ,我在其中打印如下所示的额外内容:

if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Log.d("Activity onResume", "Key: " + key + " Value: " + value);
        }
    }

Messages with both notification and data payload, both background and foreground.具有通知和数据负载的消息,包括背景和前景。 In this case the data payload is delivered to extras of the intent of your launcher activity.在这种情况下,数据有效负载被传送到您的启动器活动的意图的额外部分。 If you want to get it on some other activity, you have to define click_action on the data payload.如果您想在其他活动中获取它,则必须在数据负载上定义 click_action。 So get the intent extra in your launcher activity.因此,在您的启动器活动中获得额外的意图。

Edit:-编辑:-

From the documentation :- 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.文档中:- 在后台时,应用程序在通知托盘中接收通知有效负载,并且仅在用户点击通知时处理数据有效负载。

So check the launcher activity's oncreate() method with getIntent() extras upon click of notification.因此,在单击通知时使用 getIntent() extras 检查启动器活动的 oncreate() 方法。

So I was looking for this answer and read the same.所以我一直在寻找这个答案并阅读相同的内容。 It has to be some where.它必须在某个地方。 I know this is a year old but they still haven't really made it clear.我知道这是一岁了,但他们还没有真正说清楚。 In the pendingActivity associated with the notification ( in my case it was MainActivity ) I added the following.在与通知关联的 pendingActivity 中(在我的例子中是 MainActivity )我添加了以下内容。 Please note I was working through the Google Codelabs com.example.android.eggtimernotifications not that this was in the answers.请注意,我正在通过 Google Codelabs com.example.android.eggtimernotifications 工作,而不是在答案中。

    override fun onResume() {
        super.onResume()
        intent?.run {
            val keys = this.extras?.keySet()
            if (!keys.isNullOrEmpty()) {
                keys.forEach { key ->
                    when (this.extras!![key]) {
                        is Long -> println("$key = ${this.getLongExtra(key, 0L)}")
                        is Int -> println("$key = ${this.getIntExtra(key, 0)}")
                        is String -> println("$key = ${this.getStringExtra(key)}")
                        is Boolean -> println("$key = ${this.getBooleanExtra(key, false)}")
                        else -> println("unkonwn Type")
                    }
                }
            }
        }
    }

This resulted in the following.这导致了以下结果。

  • I/System.out: google.delivered_priority = high I/System.out: google.delivered_priority = 高
  • I/System.out: google.sent_time = 1585284914411我/System.out: google.sent_time = 1585284914411
  • I/System.out: google.ttl = 2419200我/System.out: google.ttl = 2419200
  • I/System.out: google.original_priority = high I/System.out: google.original_priority = 高
  • I/System.out: cereal = Shreddies I/System.out:谷物=碎纸片
  • I/System.out: from = /topics/breakfast I/System.out: 来自 = /topics/breakfast
  • I/System.out: milk = 1 cup I/System.out: 牛奶 = 1 杯
  • I/System.out: sugar = none I/System.out: sugar = 无
  • I/System.out: google.message_id = 0:1585284914700957%f6ddf818f6ddf818我/System.out: google.message_id = 0:1585284914700957%f6ddf818f6ddf818
  • I/System.out: collapse_key = com.example.android.eggtimernotifications I/System.out: collapse_key = com.example.android.eggtimernotifications

The data keys were cereal, from, milk and sugar.数据键是谷物、牛奶和糖。

According to documentation Cloud messaging docs message can be delivered in 3 states:根据文档Cloud messaging docs消息可以以 3 种状态传递:

  1. Notification通知
  2. Data数据
  3. Notification & Data通知和数据

Background handling:后台处理:

For 2. onMessageReceived will be called without UI and additional handling.对于 2. onMessageReceived将在没有 UI 和额外处理的情况下被调用。 Later you can show something custom.稍后你可以展示一些定制的东西。

When it comes to 3. android will show notification and hold data until there is user interaction.当涉及到3时,android会显示通知并保持数据,直到有用户交互。 After click it's just resuming your existing activity.单击后,它只是恢复您现有的活动。 Your onNewIntent is not triggered because your launch mode is not FLAG_ACTIVITY_SINGLE_TOP .您的onNewIntent未被触发,因为您的启动模式不是FLAG_ACTIVITY_SINGLE_TOP

You should also check your logcat output for: Google Play services out of date .您还应该检查您的 logcat 输出: Google Play services out of date

onMessageReceived() is not called when application is on background.应用程序在后台时不会调用 onMessageReceived()。 I have this problem few months a ago.几个月前我遇到了这个问题。 I resolve it by override handlerIntent method.我通过覆盖 handlerIntent 方法来解决它。 But your firebase messaging library should但是你的 firebase 消息库应该

implementation 'com.google.firebase:firebase-messaging:10.2.1'

after that:在那之后:

@Override
    public void handleIntent(Intent intent) {
        try
        {
            if (intent.getExtras() != null)
            {
                RemoteMessage.Builder builder = new RemoteMessage.Builder("MessagingService");

                for (String key : intent.getExtras().keySet())
                {
                    builder.addData(key, intent.getExtras().get(key).toString());
                }

                onMessageReceived(builder.build());
            }
            else
            {
                super.handleIntent(intent);
            }
        }
        catch (Exception e)
        {
            super.handleIntent(intent);
        }
    }

if you don't want to downgrade your library then如果你不想降级你的图书馆那么

You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user.您可以指定一个 click_action 来指示当用户点击通知时应启动的意图。 The main activity is used if no click_action is specified.如果未指定 click_action,则使用主要活动。

When the intent is launched you can use the启动意图时,您可以使用

getIntent().getExtras();

to retrieve a Set that would include any data sent along with the notification message.检索一个 Set,其中包含随通知消息一起发送的任何数据。

For more on notification message see https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive有关通知消息的更多信息,请参阅https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 FCM 后台应用时,额外的 Intent 数据获取 null - Extra Intent data getting null when application in background for FCM 当应用程序在 Flutter 中处于后台时,在哪里调用更新聊天消息“已交付”function? - Where to call the Update chat message "delivered" function when app is on Background in Flutter? 当应用程序处于前台或后台时如何使用 FCM 处理通知 - How to handle notifications with FCM when app is in either foreground or background 点击 FCM 通知后如何在应用程序不在后台时重新打开应用程序 - how to reopen app just when it is not in background after tap on FCM notitiction FCM 推送通知仅在应用程序处于后台时显示 - FCM Push Notifications are only displayed when app is in background FCM iOS 推送通知在应用程序终止时捕获自定义数据? - FCM iOS push notification capture custom data when app is terminated? 应用程序被杀死时 FCM 通知不会触发 - FCM Notification not firing when app is killed FCM BigQuery - 未返回 MESSAGE_DELIVERED 记录 - FCM BigQuery - No MESSAGE_DELIVERED records returned Android 当我重新打开应用程序时,深层链接再次传送 - Android deep link delivered again when I reopen the app FCM 通知仅在应用程序未打开且处于后台时在设备上发送 - FCM Notifications only sends on device when the application is not open and on background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM