使用Firebase存储ID令牌和发送Web推送通知的最佳实践 [英] Best practices for storing ID tokens and sending web push notifications using Firebase

查看:99
本文介绍了使用Firebase存储ID令牌和发送Web推送通知的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个庞大的WEBSITE推送通知世界中,我是一个新手.如果有人可以向我展示最佳实践的正确道路,我将不胜感激.再次,这是关于使用Firebase的WEBSITE推送通知.

I'm very new to this overwhelming world of WEBSITE push notifications. I would appreciate it if someone could show me the right path with the best practices. Again, it's about WEBSITE push notifications using Firebase.

我的测试网站托管在Azure上,我在带有SQL Server的c#环境中使用.Net平台.

My testing website is hosted on Azure and I use .Net plattform in c# environment with SQL server.

我已经红色并记住"了Firebase的Google文档.我还测试了如何检索令牌,刷新令牌...以及如何使用邮递员将通知发送到特定设备.

I've already red and "memorised" the Firebase google documentation. I also tested how to retrieve a token, refresh it... and send a notification to a specific device using postman.

但是我还有很多问号.

However I still have many question marks.

我的第一个问题:在服务器上收到令牌后,保存令牌的最佳做法是什么?在SQL数据库中?

My first question: What is the best practice for saving the tokens once received on the server? In an SQL database?

function sendTokenToServer(currentToken) { 
if (!isTokenSentToServer()) { 
console.log('Sending token to server...'); 
setTokenSentToServer(true); 
} else {
console.log('Token already sent to server so won\'t send it again ' + 
'unless it changes');
}

第二个问题:现在,假设我的数据库中有50.000个订阅者,什么是向其中的所有人动态发送一个推送通知而没有分组或主题的最佳实践是什么?这是最安全的传递方式POST,CURL,FETCH ...假设我正在使用FETCH,如何将相同的通知发送给这50.000个订阅者.通过数据库循环?还是?

Second question: Now let's say I have 50.000 subscribers in the database, what is the best practice to send dynamically ONE push notifications to all of them, WITHOUT GROUPING OR TOPICS? And which is the most secure delivery method POST, CURL, FETCH... Let's assume I'm using FETCH, how can I send the same notification to those 50.000 subscribers. Through a database loop? Or?

var key = 'YOUR-SERVER-KEY';
var to = 'YOUR-IID-TOKEN';
var notification = {
'title': 'Portugal vs. Denmark',
'body': '5 to 1',
'icon': 'firebase-logo.png',
'click_action': 'http://localhost:8081'
};

fetch('https://fcm.googleapis.com/fcm/send', {
'method': 'POST',
'headers': {
'Authorization': 'key=' + key,
'Content-Type': 'application/json'
 },
'body': JSON.stringify({
'notification': notification,
'to': to
})
})

.then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
})

我的第三个问题:我可以代替使用.Net和C#来完成上述向50.000个订阅者发送一个通知的方法吗?有关示例代码的任何提示吗?

My third question: Can I use instead .Net and C# to accomplish the above method of sending one single notifications to my 50.000 subscribers? Any hints about a sample code?

问一个愚蠢问题的人可能在短时间内听起来很愚蠢,但是不敢问一个愚蠢问题的人在他/她的余生中都会保持愚蠢".

"The one who asks a stupid question might sound stupid for a short time, however the one who doesn't dare to ask a stupid question will remain stupid for the rest of his/her life"

谢谢!

推荐答案

  1. 不确定要寻找的最佳实践,但是只要您能够存储令牌并确定令牌的用户,就可以了.

  1. Not sure what best practices are you looking for, but so long as you're abel to store the token and identify which user it is for, then that's about it.

如果每个令牌的消息不同,您别无选择,只能循环一个.如果是同一则消息,请使用 registration_ids 并为每个请求发送1000个令牌-仍在使用循环.使用API​​所需的请求类型为POST.

If it's a different message per token, you have no other choice but to loop each one. If it's the same message, use registration_ids and send to 1000 tokens per request -- still using a loop. The request type needed for using the API is POST.

对任何一个都没有尝试过,抱歉.但是,只要您可以发送HTTP POST请求,就可以了.

Haven't tried it on either one, sorry. But so long as you could send an HTTP POST Request, then that's it.

这篇关于使用Firebase存储ID令牌和发送Web推送通知的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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