简体   繁体   English

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

[英]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");
  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?由于返回值为void ,问题是我如何理解订阅成功?
  2. Is it a bad practice to call 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:给定客户端应用程序实例的注册令牌,您可以执行以下操作:

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.

相关问题 Firebase云消息传递将多个令牌订阅到主题 - Firebase Cloud Messaging Subscribe Multiple Tokens to a Topic Android,FireBase 云消息传递,(FCM) - Android, FireBase Cloud Messaging, (FCM) 如果主题为字符串值,则无法订阅 Firebase 云消息主题 - Cannot subscribe to Firebase Cloud Messaging Topic if the topic is in String values FireBase云消息传递(FCM)订阅/取消订阅主题OFFLINE时 - FireBase Cloud Messaging (FCM) subscribing/unsubscribing to topic When OFFLINE 如何使用 MainActivity.java 文件夹中的 android 工作室(Firebase 云消息传递)订阅主题? - How to subscribe to a topic using android studio (Firebase Cloud Messaging) in the MainActivity.java folder? 订阅的Firebase云消息传递问题//取消订阅主题 - Firebase Cloud Messaging issue with subscribe//unsubscribe from topic 使用 FCM(Firebase 云消息传递)通过 C# 将推送发送到 Android - Send push to Android by C# using FCM (Firebase Cloud Messaging) Android Firebase Cloud Messaging(FCM):subscribeToTopic会自动重试吗? - Android Firebase Cloud Messaging(FCM): Will subscribeToTopic do automatic retries? Android Firebase 云消息传递令牌 (FCM) 令牌太短/不完整 - Android Firebase Cloud Messaging Token (FCM) token is too short/incomplete 在Android上实施FCM(Firebase Cloud Messaging)后,应用在启动时崩溃了吗? - App crashing on launch after the implementation of FCM(Firebase Cloud Messaging) on android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM