了解 Firebase 消息传递的客户端流程

Abh*_*ury 6 android push-notification firebase firebase-cloud-messaging

我正在尝试深入研究 FCM 消息的客户端流程(在 Android 设备上收到消息后)。

我知道 Android 有一些本机 Firebase 系统服务,最初接收 Firebase 消息(如果设备未离线)。此消息如何传递到预期的应用程序(使用广播、意图?)?这个原生 Firebase 服务如何知道将消息发送到哪个应用程序?

文档中提到,即使在停止时FirebaseMessagingService,设备上的 也可以接收 Firebase 消息。在这种情况下它是如何工作的?

在一个应用程序中,可以有一个FirebaseMessagingService但多个 FirebaseApp 实例。应用的这些不同 FirebaseApp 实例如何影响消息传递?

我很感激对流程的详细了解,我找不到任何关于此的官方文档/博客。

dnh*_*yde 3

有关他们不久前添加的流程的非常全面的概述,FCM 架构概述也许您已经找到了。

\n

具体到 Android,让我们首先研究问题的这一部分:\n-> "文档中提到,设备上的 FirebaseMessagingService 即使在停止时也可以接收 Firebase 消息。它是如何工作的这个案例?

\n

具有在活动停止后仍然存在的后台服务。“本机 Firebase 系统服务”是在 firebase SDK Manifest 中声明的服务,它通过Manifest merging集成到您的应用程序中。

\n
\n

Your APK file can contain just one AndroidManifest.xml file, but your Android Studio project may contain several\xe2\x80\x94provided by the main source set, build variants, and imported libraries

\n
\n

As you can see the Firebase SDK Manifest registers the service. Note that the intent filter has a very low (-500) priority, 0 is the default value. So when you extend the firebase service with .MyService your service will be called, check "Edit your Manifest" step from the official guide:

\n
\n

A service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background.

\n
\n

If you do not do that your service will never be called and the notification display will be managed by the firebase service.

\n

But how does the service receive the events? The best guess is by some socket or polling mechanism as suggested here, another hint is that firebase periodically closes down the connection to "perform load balancing" as reported in their protocol, the SDK class performing this "polling" is probably TopicSyncTask.

\n

Back to us, your service is "polling" in background, when there is an event to show it creates a notification using the system\'s NOTIFICATION_SERVICE:

\n
// Register the channel with the system\nval notificationManager: NotificationManager =\n       getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager\n\n
Run Code Online (Sandbox Code Playgroud)\n

-> "How is this message delivered to the intended application (using broadcasts, intent?)?"

\n

Through an Intent with an intent filter: it is your service that creates a notification to send to the Android OS, adding an intent filter with the specific action com.google.firebase.MESSAGING_EVENT Android OS at this point will look for any Activity or Service that has an intent-filter with a the same action. If you extended the firebase service your service is called by overriding the onMessageReceived as shown in firebase guide.

\n

这是否意味着任何应用程序都可以创建触发您的服务的意图?否,因为firebase SDK Manifest设置了android:exported=false.

\n

-> “这个原生 Firebase 服务如何知道将消息发送到哪个应用程序?

\n

该服务知道您的应用程序唯一的包名称,并在创建通知时使用它。

\n

->“应用程序的这些不同 FirebaseApp 实例如何影响消息传递?

\n

不确定我是否理解正确,但此类任务将通过 解决,如果您需要访问应用程序中的多个项目,google-services.json有一个官方指南。

\n

总结一下流程是:

\n
    \n
  1. firebase后台服务轮询事件(由SDK启动)
  2. \n
  3. 该服务创建一个针对任何活动/服务的意图,以过滤com.google.firebase.MESSAGING_EVENT
  4. \n
  5. 您的服务onMessageReceived被调用(如果您没有扩展 firebase 服务,则 firebaseonMessageReceived则将调用 firebase)
  6. \n
  7. 您的服务处理通知(如果您没有扩展 firebase 服务,它会调用您的应用程序,因为它知道您的应用程序包名称)
  8. \n
\n