且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:订阅Firebase云消息传递(FCM)主题

更新时间:2022-11-26 13:37:46


1。如何才能理解订阅是成功的?


文档中没有明确提到收到的响应订阅成功时。然而,如果您需要强制所有用户订阅特定主题,则应该调用 subscribeToTopic $ c $在你的应用程序的第一次安装。这很可能会确保有一个连接到互联网(因为它可能是通过Play商店下载和安装)和订阅成功。



然而,如果你想确定,你也可以通过你自己的App服务器来处理。正如文档中所述:


您可以利用实例ID API从服务器端执行基本主题管理任务。给定客户端应用实例的注册令牌,您可以执行以下操作:


检查注册令牌,如果它们没有被成功地分派给您的主题,则发送通知给它将触发的地方您的客户端应用程序调用 subscribeToTopic


2。每次我的应用程序启动时调用subscribeToTopic是一个不好的习惯吗?


编辑:从评论部分添加它:订阅应用程序启动应罚款。



谢谢@FrankvanPuffelen验证。 :)

According to Firebase cloud messaging documentation, for subscribing a user to a topic I need to call

FirebaseMessaging.getInstance().subscribeToTopic("news");

  1. In my application, I need all users to be subscribed to my cloud messaging topic. Since return value is void, the question is how can I understand that subscription was successful?
  2. Is it a bad practice to call subscribeToTopic each time my application starts?

1. ..how can I understand that subscription was successful?

There is nothing explicitly mentioned in the docs about a response received when the subscription is successful.

However, if you need to mandate all of your users to be subscribed to a specific topic, you should call the subscribeToTopic on your app's first install. This will most likely make sure that there is a connection to the internet (since it's probably been downloaded and installed via the Play Store) and the subscription successful.

However, if you want to make sure, you can also handle he checking via your own App Server. As mentioned in the docs:

You can take advantage of Instance ID APIs to perform basic topic management tasks from the server side. Given the registration token(s) of client app instances, you can do the following:

Check through the registration tokens, if they haven't been successfully subsribed to your topic, send a notification to it where it will trigger your client app to call subscribeToTopic.

2. Is it a bad practice to call subscribeToTopic each time my application starts?

Edit: Adding it in from the comments section: Subscribing on app start should be fine.

Thank you @FrankvanPuffelen for verifying. :)