带有 FCM 的 Android 通知 - 谁启动了 FirebaseMessagingService? [英] Android Notification with FCM - Who started the FirebaseMessagingService?

查看:17
本文介绍了带有 FCM 的 Android 通知 - 谁启动了 FirebaseMessagingService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据设置指南此处,在示例应用中, A) 我创建了一个 extends firebase 服务类的类.B) 我把这些类放在 AndroidManifest.xml

A) Java 类

public class MyFirebaseMessagingService 扩展 FirebaseMessagingService {@覆盖public void onMessageReceived(RemoteMessage remoteMessage) {//a) 服务的生命周期是什么?如何确保调用此方法?//b) 如果服务在某个时候被终止,系统会在收到新消息时重新启动它吗?Log.d(TAG, "From: " + remoteMessage.getFrom());}}

B) AndroidManifest.xml

<服务android:name=".MyFirebaseMessagingService"><意图过滤器><action android:name="com.google.firebase.MESSAGING_EVENT"/></意图过滤器></服务>

然后,该应用可以接收来自 FCM 的通知!

这是我的问题:

  1. 谁启动了 FirebaseMessagingService 通知服务?一定有什么地方在调用startService(),不是吗?

  2. 我的理解是,无论我的应用处于何种状态,下游通知都会传送到我的移动设备的 google play 服务.然后,当 appserviceswipe closed/killed/时,我可以在多大程度上确保我的 onMessageReceive() 被调用强制停止?相关行为是否记录在任何地方?

  1. 我更关心以下案例.我们来谈谈数据消息.(与通知消息相比.)我的客户端应用程序收到 FCM 消息并显示默认通知,因为 google play 服务正在运行并将其发送到系统托盘.但是我的 onMessageReceive() 没有被调用,因为 MyFirebaseMessagingService 被系统杀死并且没有重新启动.是否可以?

解决方案

1.谁启动了 FirebaseMessagingService 通知服务?一定有什么地方在调用startService(),不是吗?

SDK 会自动为您执行此操作.

<块引用>

2.我的理解是,无论我的应用程序处于何种状态,下游通知都将传送到我的移动设备的 google play 服务.然后,当 appserviceswipe closed/killed/时,我可以在多大程度上确保我的 onMessageReceive() 被调用强制停止?相关行为是否记录在任何地方?

接收 Android 消息的官方文档在这里.它讨论了当您的应用处于前台和后台时的消息行为(取决于您使用的消息负载类型(即 notificationdata)).>

为了能够自己处理消息(在 onMessageReceived() 中),您必须发送data-only 消息有效负载.

关于滑动关闭/杀死/强制停止的话题,这个话题已经讨论了很长时间,似乎没有一个明确的答案.在我的一次测试中,如果我,我仍然能够接收消息(使用data-消息负载测试)滑动关闭我的应用.但是当我从设置"菜单强制关闭它时,我无法接收任何消息.请注意,这并非总是这种行为.

有些设备被设计为当您滑动关闭应用程序时,它会与强制停止它们相同(请参阅我的回答此处).

还有一些设备,即使应用程序仍然只是简单地滑动,即使它没有强制关闭,设备本身也会阻止它接收消息.其他人则说情况并非如此,因为像 WhatsApp 这样的应用程序能够做到这一点.到目前为止,我之所以了解到这一点,是因为设备制造商已将大多数知名应用列入白名单,使其成为可能.

所以只是回答你的问题,不.这在任何地方都没有记录,因为 (IMO) 这是一个也取决于设备的主题,并且 FCM 无法完全控制.

<块引用>

3.我比较关心下面这个案例.我们来谈谈数据消息.(与通知消息相比.)我的客户端应用程序收到 FCM 消息并显示默认通知,因为 google play 服务正在运行并将其发送到系统托盘.但是我的 onMessageReceive() 没有被调用,因为 MyFirebaseMessagingService 被系统杀死并且没有重新启动.有可能吗?

AFAIK,FirebaseMessagingService 与 Google Play 服务绑定,只要 Google Play 服务处于活动状态,Firebase 服务也将处于活动状态.我此处的答案的一部分:

<块引用>

但是,我已经看到之前针对 FirebaseMessagingService 提到过它(请参阅此帖子,@ArthurThompson 的评论):

<块引用>

这些服务将由始终在设备上运行的 Google Play 服务启动.您不必也不应该自己启动/停止这些服务.

According to the set up guide here, in a sample app, A) I created a class that extends firebase services class. B) I put these classes in the AndroidManifest.xml

A) Java Class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //a) What's the life cycle of the service? How can I ensure this method is getting called?
        //b) If the service is killed at some point, will system restart it on receiving new messages?
        Log.d(TAG, "From: " + remoteMessage.getFrom());
    }
}

B) AndroidManifest.xml

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

Then, the app can receive notifications from FCM!

Here is my question:

  1. Who started the FirebaseMessagingService notification service? There must be some place calling startService(), isn't it?

  2. My understanding is that, the downstream notification will deliver to my mobile device's google play service, regardless of the state of my app. Then, to what extent can I ensure that my onMessageReceive() is called when the app or service is swipe closed/killed/force stop? Is the relevant behavior documented anywhere?

EDIT:

  1. I am more concerned about the following case. Let's talk about the data message. (compared to the notification message.) My client app received the FCM message and shows the default notification, because google play service is running and send it to the system tray. But my onMessageReceive() is not called, because MyFirebaseMessagingService is killed by system and not restarted. Is it possible?

解决方案

1. Who started the FirebaseMessagingService notification service? There must be some place calling startService(), isn't it?

The SDK does this automatically for you.

2. My understanding is that, the downstream notification will deliver to my mobile device's google play service, regardless of the state of my app. Then, to what extent can I ensure that my onMessageReceive() is called when the app or service is swipe closed/killed/force stop? Is the relevant behavior documented anywhere?

The official docs for Receiving Message for Android is here. It discusses the behavior of the message (depending on the type of message payload you use (i.e. notification or data)) for when your app is in foreground and background.

To be able to handle the message yourself (in onMessageReceived()), you'll have to send data-only message payloads.

For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.

There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).

There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible.

So just to answer your question, no. This is not documented anywhere because (IMO) this is a topic that depends also on the device and that FCM has no total control over.

3. I am more concerned about the following case. Let's talk about the data message. (compared to the notification message.) My client app received the FCM message and shows the default notification, because google play service is running and send it to the system tray. But my onMessageReceive() is not called, because MyFirebaseMessagingService is killed by system and not restarted. Is it possible?

AFAIK, the FirebaseMessagingService is tied with the Google Play Service so so long as Google Play Service is active, so will the Firebase service. A portion from my answer here:

However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by @ArthurThompson):

These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself.

这篇关于带有 FCM 的 Android 通知 - 谁启动了 FirebaseMessagingService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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