fcm 订阅主题 [英] fcm subscribe to topic

查看:125
本文介绍了fcm 订阅主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FCM 向所有主题订阅者发送通知.首先,我需要为用户订阅一个主题.我的工作(我使用 JavaScript Firebase Cloud Messaging 发送网络推送通知):

I'm trying to use FCM to send notifications to all topic subscribers. First of all, I need to subscribe the user to a topic. What I do (I'm using JavaScript Firebase Cloud Messaging for web push notifications):

1) 获取 FCM 实例

1) Get FCM instance

var messaging = firebase.messaging();

2) 获取令牌

messaging.getToken();

3) 通过ajax向服务器发送token

3) Send token to server by ajax

4) 为客户端订阅来自服务器的主题

4) Subscribe the client to a topic from a server

https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME

所以,我的 js 代码是这样的:

So, my js code is smth like this:

            var messaging = firebase.messaging();
            messaging.useServiceWorker(reg);

            messaging.requestPermission()
                .then(function () {
                    messaging.getToken()
                        .then(function (currentToken) {
                            if (currentToken) {
                                $.ajax({
                                    type   : 'POST',
                                    url    : URL_TO_SERVER_METHOD
                                    data   : {
                                        token : currentToken
                                    }
                                });

                            }
                        })
                })

然后,如果我向 https://fcm.googleapis.com/fcm/send 提出请求 使用所有必要参数后,我会在浏览器中收到通知.一切正常,但我从 fcm 文档中读到令牌有时会被 fcm 应用程序刷新.这意味着如果令牌被刷新,但用户没有重新订阅该新令牌,他将不会收到通知.

Then, if I make a request to https://fcm.googleapis.com/fcm/send with all necessary parameters, I get a notification in my browser. Everything works fine, but I've read from fcm docs that tokens are refreshed sometimes by an fcm app. This means if the token was refreshed, but the user wasn't resubscribed with that new token, he wouldn't get notifications.

  • 问题 1:

如何确保我的订阅者始终收到通知?

How can I be sure my subscribers always get notifications?

  • 问题 2:

我已经看到了一些关于 onTokenRefresh 方法的内容,但是当浏览器关闭或计算机关闭时它会被调用吗(我敢肯定,不会)?以及如何模拟令牌刷新操作?

I've seen something about the onTokenRefresh method, but will it be called when a browser is closed or computer is turned off (I'm sure, no)? And how can a simulate token refresh action?

  • 问题 3:

我只能订阅一次(下次不需要使用相同的令牌),但我不知道我的令牌是否旧,我必须重新订阅当前用户.这种情况我该怎么办(与之前的问题有关)?

I can do subscription only once (next times with the same token aren't needed), but I don't know if my token is old and I have to resubscribe current user. What should I do this case (it's related to previous questions)?

推荐答案

要确保您的用户始终订阅,您肯定需要使用 onTokenRefresh 方法.onTokenRefersh 只会在用户拥有新令牌时触发.

To make sure your users are always subscribed you definitely need to use the onTokenRefresh method. onTokenRefersh will only get triggered when the user has a new token.

代码看起来像那样

messaging.onTokenRefresh(function () {

    messaging.getToken()
        .then(function (refreshedToken) {
            console.log('Token refreshed.');
            // Indicate that the new Instance ID token has not yet been sent to the
            // app server.
            setTokenSentToServer(false);
            // Send Instance ID token to app server.
            sendTokenToServer(refreshedToken);
        })
        .catch(function (err) {
            console.log('Unable to retrieve refreshed token ', err);               
        });
});

这篇关于fcm 订阅主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆