检查您是否订阅了主题

检查您是否订阅了主题

本文介绍了Android / Firebase - 检查您是否订阅了主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法测试你是否订阅了android方面的主题。

I am wondering if there is a way to test to see if you are subscribed to a topic on the android side of things.

基本上,我是 HOPING 当设备首次获得令牌时,所有设备将在安装期间订阅主题。但是,设备总是有可能无法订阅。 FCM注册令牌应该在设备上安装很长时间,因此,在不清除数据,卸载/重新安装等的情况下,不应再次调用onTokenRefresh()方法。

Basically, I am HOPING that all devices will subscribe to a topic during their installation, when the token is first obtained by the device. However, there is always a chance that the device fails to subscribe. The FCM registration token should be installed on the device for a long time, and thus, the onTokenRefresh() method shouldn't be called again without clearing data, uninstall/reinstall, etc.

我的想法是测试设备是否订阅了我的MainActivity中的主题,如果没有,则尝试再次订阅。如果订阅失败,则获取新令牌并再试一次等​​。

My idea was to test to see if the device is subscribed to a topic in my MainActivity, and if not, then try to subscribe again. If it fails to subscribe, then get a new token and try again, etc.

    @Override
    public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.e(TAG, "Refreshed token: " + refreshedToken);

    // Subscribe to a topic
    Log.e(TAG, "Subscribing to topic");
    FirebaseMessaging.getInstance().subscribeToTopic("test");

因此,我可以订阅和取消订阅,但如何检查设备是否订阅了某个主题?不幸的是,我公平分享谷歌搜索,但找不到任何东西。

So, I can subscribe and unsubscribe, but how do I check if the device is subscribed to a topic? I did my fair share of googling, and couldn't find anything, unfortunately.

我非常感谢任何/所有的帮助。谢谢!

I would greatly appreciate any/all assistance. Thanks!

推荐答案

目前无法检查客户端是否订阅了某个主题。

There is currently no way to check on the client side if they are subscribed to a topic.

subscribeToTopic 的行为是它会立即订阅指定的主题,如果失败,它会自行重试(除非你的应用程序被杀了)。请参阅我的回答。

The behavior for subscribeToTopic is it would immediately subscribe to the specified topic, if it fails, it would retry on it's own (unless your app was killed). See my answer here.

我认为强迫 onTokenRefresh 调用只是为了确保 subscribeToTopic 太多了。如果你愿意的话,你可以简单地在初始活动中调用它,这样,每次应用程序启动时,它都会发送订阅请求。

I think that forcing the onTokenRefresh call just to make sure that subscribeToTopic is too much. You could simply just call it in your initial activity if you want, that way, everytime the app starts, it sends the subscription request.

这篇关于Android / Firebase - 检查您是否订阅了主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:18