根据 Firebase 云消息传递文档,订阅用户到我需要调用的主题
According to Firebase cloud messaging documentation, for subscribing a user to a topic I need to call
FirebaseMessaging.getInstance().subscribeToTopic("news");
void
,问题是如何我可以理解订阅成功了吗?subscribeToTopic
是不是一种不好的做法?应用程序启动了吗?void
, the question is how
can I understand that subscription was successful?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) 主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!