[英]Android: Subscribe to Firebase Cloud Messaging(FCM) Topic
According to Firebase cloud messaging documentation , for subscribing a user to a topic I need to call根据Firebase 云消息传递文档,为了让用户订阅我需要调用的主题
FirebaseMessaging.getInstance().subscribeToTopic("news");
void
, the question is how can I understand that subscription was successful?由于返回值为void
,问题是我如何理解订阅成功?subscribeToTopic
each time my application starts?每次我的应用程序启动时都调用subscribeToTopic
是一种不好的做法吗?1. How can I understand that subscription was successful? 1、如何知道订阅成功?
Edit:编辑:
You could now check if subscription is successful by adding addOnSuccessListener()
您现在可以通过添加addOnSuccessListener()
检查订阅是否成功
FirebaseMessaging.getInstance().subscribeToTopic("news").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
}
});
Original:原文:
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.但是,如果您需要强制所有用户订阅特定主题,则应在首次安装应用时调用 subscribeToTopic
。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.这很可能确保连接到互联网(因为它可能是通过 Play 商店下载和安装的)并且订阅成功。
However, if you want to make sure, you can also handle he checking via your own App Server.但是,如果您想确定,您也可以通过您自己的 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.您可以利用实例 ID API 从服务器端执行基本的主题管理任务。Given the registration token(s) of client app instances, you can do the following:给定客户端应用程序实例的注册令牌,您可以执行以下操作:
Find out details about a client app instance's subscriptions, including each topic name and subscribe date.了解有关客户端应用程序实例订阅的详细信息,包括每个主题名称和订阅日期。See Get information about app instances .请参阅获取有关应用程序实例的信息。
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
.检查注册令牌,如果它们尚未成功订阅您的主题,请向其发送通知,它将触发您的客户端应用程序调用subscribeToTopic
。
2. Is it a bad practice to call subscribeToTopic each time my application starts? 2. 每次我的应用程序启动时都调用 subscribeToTopic 是一种不好的做法吗?
Edit: Adding it in from the comments section: Subscribing on app start should be fine.编辑:从评论部分添加它:在应用程序启动时订阅应该没问题。
Thank you @FrankvanPuffelen for verifying.谢谢@FrankvanPuffelen 的验证。 :) :)
I have written this function and tested.我已经编写了这个函数并进行了测试。 May be helpful.可能会有所帮助。
private void subscribeToMessaging(){
SharedPreferences prefs = getSharedPreferences(SETTINGS_TITLE, MODE_PRIVATE);
// Getting value from shared preferences
boolean isSubscriptionEnable = prefs.getBoolean(SETTING_NOTIFICATION, true);
// if "isSubscriptionEnable" is true then check whether its already subscribed or not
if (isSubscriptionEnable){
boolean alreadySubscribed = prefs.getBoolean(SETTING_ALREADY_SUBSCRIBED, false);
// if not already subscribed then subscribe to topic and save value to shared preferences
if (!alreadySubscribed){
FirebaseMessaging.getInstance().subscribeToTopic("global").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
}
});
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_TITLE, MODE_PRIVATE).edit();
editor.putBoolean(SETTING_ALREADY_SUBSCRIBED, true);
editor.apply();
Toast.makeText(this, "Subscribed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Already subscribed", Toast.LENGTH_LONG).show();
}
}
}
Don't forget to write these lines above onCreate()不要忘记在 onCreate() 上面写这些行
public static final String SETTINGS_TITLE = "settings";
public static final String SETTING_NOTIFICATION = "notification_state";
public static final String SETTING_ALREADY_SUBSCRIBED = "already_subscribed";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.