在收到 Firebase 通知时打开应用 (FCM) [英] Open app on firebase notification received (FCM)

查看:25
本文介绍了在收到 Firebase 通知时打开应用 (FCM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在收到通知时自动打开应用程序,这可以通过 Firebase 和新的 FCM 通知实现吗?

I want to open application automatically when notification is received, is this possible with Firebase and new FCM notifications?

我知道我可以设置 click_action 但这只是为了自定义在点击通知时启动哪个活动,我需要在收到通知时自动启动的东西.

I know I can set click_action but that's only for customizing which activity will start on notification click, I need something that will start automatically when notification is received.

我尝试了快速启动消息传递 firebase 示例,并且有一个 onMessageReceived() 方法,但它仅在应用程序处于前台时才有效.应用程序在后台时是否也会执行某些操作?GCM 可以通过直接从收到通知时调用的广播接收器启动活动意图来执行我想要的操作.

I tried the quick start messaging firebase sample and there is a onMessageReceived() method but it only works if the app is in foreground. Is there something that will execute while app is in background as well? GCM could do something like what I want here by directly starting activity intent from broadcast receiver which is called when notification is received.

推荐答案

快速解答:

要通过 FCM 自动打开应用程序,您需要使用 data-message,它保证始终调用 FirebaseMessagingService.onMessageReceived() 方法.

Quick answer:

To automatically open an application via FCM you need to use a data-message, which guarantees to always invoke the FirebaseMessagingService.onMessageReceived() method.

然后您可以在 .onMessageReceived() 方法中添加您的逻辑以启动首选活动.

Then you can add your logic in the .onMessageReceived() method to start the preferred activity.

警告:在没有任何用户交互的情况下启动 UI 对于大多数应用程序来说是一种非常非常糟糕的做法!请在此处阅读 MarkG 答案:如何从服务启动活动?

WARNING: launching a UI without any user interaction is a very very bad practice for most of the applications! Please read the MarkG answer here: How to start an Activity from a Service?

[...] 中断用户当前正在做的事情被认为是糟糕的设计形式,尤其是从应该在后台运行的事情中.
因此,当用户决定是时候进行调查时,您应该考虑使用通知 [...] 来启动所需的活动.[...]

[...] Interrupting what the user is currently doing is considered bad design form, especially from something that is supposed to be operating in the background.
Therefore, you should consider using a Notification [...] to launch the desired Activity when the user decides it is time to investigate. [...]

完整解释:

FCM 的工作原理与 GCM 类似,可以接收两种类型的消息:

Full explaination:

FCM works similarly to GCM and can receive two types of messages:

  1. 显示消息:
    有效负载{"notification" : { "body" : "hello world"}}
    这些消息在应用处于后台时自动显示,如果应用已经处于前台,它们会调用 FirebaseMessagingService.onMessageReceived().

  2. 数据消息:
    有效载荷 {"data" : { "key1" : "value1"}}
    这些消息总是调用 FirebaseMessagingService.onMessageReceived()
    即使应用已关闭或在后台运行.
  1. display-messages:
    payload {"notification" : { "body" : "hello world"}}
    These messages are automatically displayed when the app is in background and they call FirebaseMessagingService.onMessageReceived() if the app is already in foreground.

  2. data-messages:
    payload {"data" : { "key1" : "value1"}}
    These messages always invoke FirebaseMessagingService.onMessageReceived(),
    even if the app is closed or in background.

click_action 是通知负载的一个参数,因此它适用于显示消息.

click_action is a parameter of the notification payload, thus it applies to the display-messages.

表示与用户点击通知相关联的操作.
如果这是设置一个具有匹配意图过滤器的活动在用户点击通知时启动.

Indicates the action associated with a user click on the notification.
If this is set an activity with a matching intent filter is launched when user clicks the notification.

https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

这篇关于在收到 Firebase 通知时打开应用 (FCM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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