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

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

问题描述

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

我知道我可以设置click_action,但这只适用于自定义哪些活动将开始通知点击,我需要的东西,会收到通知时自动启动。

我试着快速启动消息传递的Firebase示例,并有一个onMessageReceived()方法,但它只适用于应用程序在前台。有什么东西会执行而应用程序在后台以及?
GCM可以通过直接启动广播接收器中的活动意图来实现我想要的功能,当接收到通知时调用它。

解决方案

快速回答:



要通过FCM自动打开应用程序,您需要使用数据消息 ,它保证总是调用 FirebaseMessagingService.onMessageReceived()方法。

然后你可以添加你的逻辑 .onMessageReceived()方法启动首选活动。 警告:没有任何用户交互的UI对于大多数应用程序来说是非常糟糕的做法!请阅读 MarkG 答案:
如何从服务启动一个活动?


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



详细解释:

GCM,可以收到两种类型的消息:
$ b $ ol

  • display-messages:
    有效负载
    {notification:{body:hello world}}

    这些消息在应用程序处于后台时自动显示,如果应用程序已经在前台,它们会调用 FirebaseMessagingService.onMessageReceived()


  • :value1}}

    这些消息始终会被调用 FirebaseMessagingService.onMessageReceived()

    ,即使应用程序已关闭或处于后台。

    click_action code>是通知有效载荷的参数,因此它适用于显示信息。


    表示与a用户点击通知。

    如果设置了该选项,则当用户点击通知时,会启动匹配意图过滤器
    的活动


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


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

    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.

    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.

    解决方案

    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.

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

    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. [...]

    Full explaination:

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

    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 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天全站免登陆