如何使用Firebase Cloud Messaging通过Web浏览器订阅主题 [英] How to subscribe to topics with web browser using Firebase Cloud Messaging

查看:483
本文介绍了如何使用Firebase Cloud Messaging通过Web浏览器订阅主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法,使用Firebase Cloud Messaging将通知发送给我的所有应用的用户,但是我拥有一个仅限网络的应用。我见过的解决方案似乎是Android / iOS,基本上涉及让用户自动订阅一个名为allDevices的主题,然后向订阅该主题的所有用户发送通知。我似乎无法找到任何有关如何让一个基于Web的用户订阅一个主题的文档。有没有人知道这是可能的,如果是的话,是否有文件,我已经错过了,将覆盖?



谢谢!


通过REST API为某个主题订阅令牌需要服务器密钥的用户

。由于服务器密钥允许代表应用程序向所有应用程序的用户发送消息,因此只能从您控制的进程安全地完成。



事实证明,在我的测试中,我使用了一个较旧的项目,客户端API键允许订阅主题。此功能已从新项目中删除,出于安全原因。



原始答案

您可以调用REST API来执行此操作,具体为创建关系映射对于一个应用程序实例
$ b $ pre $ $ $ $ $ $ $ function $ subscribeTokenToTopic(token,topic){
fetch('https: //iid.googleapis.com/iid/v1/'+token+'/rel/topics/'+topic,{
method:'POST',
headers:new Headers({
'Authorization':'key ='+ config.apiKey
})
})。then(response => {
if(response.status< 200 || response.status> ; = 400){
throw'订阅主题时出错:'+ response.status +' - '+ response.text();
}
console.log('订阅'' + topic +'');
})。catch(error => {
console.e rror(error);
})
}

其中 config 是Firebase Web应用程序的配置片段,因此 config.apiKey public api键你的应用程序。


I'm trying to find a way to send a notification using Firebase Cloud Messaging to all of my app's users, but I have a web-only app. I've seen solutions that appear to be for Android/iOS, which basically involves having the users auto-subscribe to a topic called "allDevices" and then sending a notification to all users subscribed to that topic. I just can't seem to find any documentation on how to have a web-based user subscribe to a topic. Does anyone know if that's possible, and if it is, is there documentation that I've missed that would cover that?

Thanks!

解决方案

Update

Subscribing a token to a topic through the REST API requires the user of the server key. Since having the server key allows sending messages on your app's behalf to all of your app's users, this can only be securely done from a process that you control.

It turns out that in my tests I was using an older project, where client API keys were allows to subscribe to topics. This ability has been removed from newer projects for security reasons.

Original answer below:

You can call the REST API to do so, specifically create a relation mapping for an app instance:

function subscribeTokenToTopic(token, topic) {
  fetch('https://iid.googleapis.com/iid/v1/'+token+'/rel/topics/'+topic, {
    method: 'POST',
    headers: new Headers({
      'Authorization': 'key='+config.apiKey
    })
  }).then(response => {
    if (response.status < 200 || response.status >= 400) {
      throw 'Error subscribing to topic: '+response.status + ' - ' + response.text();
    }
    console.log('Subscribed to "'+topic+'"');
  }).catch(error => {
    console.error(error);
  })
}

Where config is the configuration snippet for your Firebase web application, so config.apiKey is the public api key for your app.

这篇关于如何使用Firebase Cloud Messaging通过Web浏览器订阅主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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