Android:订阅 Firebase Cloud Messaging(FCM) 主题

Android: Subscribe to Firebase Cloud Messaging(FCM) Topic(Android:订阅 Firebase Cloud Messaging(FCM) 主题)
本文介绍了Android:订阅 Firebase Cloud Messaging(FCM) 主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Firebase 云消息传递文档,订阅用户到我需要调用的主题

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

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

  1. 在我的应用程序中,我需要所有用户都订阅我的云消息传递主题.由于返回值为 void,问题是如何我可以理解订阅成功了吗?
  2. 每次我都调用 subscribeToTopic 是不是一种不好的做法?应用程序启动了吗?
  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.我怎么知道订阅成功了?

您现在可以通过添加 addOnSuccessListener()

FirebaseMessaging.getInstance().subscribeToTopic("news").addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
        }
    });

原文:

文档中没有明确提及订阅成功时收到的响应.

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

但是,如果您需要强制所有用户订阅特定主题,则应在首次安装应用时调用 subscribeToTopic.这很可能会确保连接到互联网(因为它可能是通过 Play 商店下载和安装的)并且订阅成功.

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.

但是,如果您想确定,您也可以通过自己的 App Server 处理他的检查.如 docs 中所述:

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

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

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:

  • 了解有关客户端应用实例订阅的详细信息,包括每个主题名称和订阅日期.请参阅获取有关应用实例的信息.

检查注册令牌,如果它们没有成功订阅您的主题,向它发送通知,它将触发您的客户端应用调用 subscribeToTopic.

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.每次我的应用程序启动时调用 subscribeToTopic 是不是一种不好的做法?

从评论部分添加:在应用启动时订阅应该没问题.

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

感谢@FrankvanPuffelen 的验证.:)

Thank you @FrankvanPuffelen for verifying. :)

这篇关于Android:订阅 Firebase Cloud Messaging(FCM) 主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)