使用FCM进行Android通知 - 谁启动了FirebaseMessagingService? [英] Android Notification with FCM - Who started the FirebaseMessagingService?

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

问题描述

根据设置指南此处, A)我创建了一个类 extends firebase services类。 B)我把这些类放在 AndroidManifest.xml中


$ b $ a)Java Class

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






$ b $
$ b

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

然后,应用程序可以收到来自FCM的通知!



这是我的问题:


  1. 谁启动了FirebaseMessagingService通知服务?
    必须有一些地方叫做startService(),不是吗?我的理解是,无论我的应用程序状态如何,下游通知都会传递到我的移动设备的Google Play服务。
    然后,当应用程序被调用时,我可以在多大程度上确保我的 onMessageReceive() code> service
    swipe closed / killed / force stop ?相关行为记录在任何地方吗?



  2. 编辑:


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

      解决方案


      1。谁启动了FirebaseMessagingService通知服务?必须有一些地方调用startService(),不是吗?

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


      2。我的理解是,无论我的应用程序状态如何,下游通知都会传递到我的移动设备的Google Play服务。那么,当 app 被调用时,我可以在多大程度上确保我的 onMessageReceive() > service swipe closed / killed / force stop ?是否有相关的行为记录在任何地方?


      官方文档 Android的接收消息在这里。它讨论了消息的行为(取决于你使用的消息有效载荷的类型(即 notification data ))为了能够自己处理消息(在 onMessageReceived()),你必须发送 data - only 消息有效载荷。

      关于 swipe closed / killed / force stopped 问题,这个话题已经讨论了相当长的一段时间了,似乎没有一个明确的答案。在我的一个测试中,我可以仍然收到一条消息(仅用数据 - 消息有效内容进行测试)如果我滑动关闭我的应用程序。但是,当我从设置菜单强制关闭时,I 无法收到任何消息。请注意,这始终是 的行为。 有些设备设计成当您 swipe close 应用程序,它将和停止一样(请参阅我的回答这里)。



      即使应用程序仍然只是简单地将它们刷掉,但即使它不是强制关闭,设备本身阻止它接收消息。其他人则认为这不可能,因为像WhatsApp这样的应用程序能够做到这一点。我之所以学到这一点,是因为设备制造商已经把大部分知名应用列入了白名单。



      <所以只是为了回答你的问题,不。这是没有记录任何地方,因为(国际海事组织)这是一个主题,也取决于设备和FCM没有总控制


      3。我更关心下面的情况。我们来谈谈数据信息。 (与通知消息相比)。我的客户端应用程序收到FCM消息,并显示默认通知,因为谷歌播放服务正在运行,并将其发送到系统托盘。但是我的onMessageReceive()没有被调用,因为MyFirebaseMessagingService被系统终止而没有重新启动。是否有可能?


      AFAIK,FirebaseMessagingService与Google Play服务捆绑只要Google Play服务处于活动状态,Firebase服务也一样。从我的在这里回答的一部分:


      不过,我已经看到之前提到的 FirebaseMessagingService (见



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