[英]How to send push notifications to multiple device with FCM tokens using cloud functions
First I generated a FCM token and stored in firestore. 首先,我生成了FCM令牌并将其存储在Firestore中。 After that I wrote a cloud functions to send notifications based on FCM token.
之后,我编写了一个云函数来基于FCM令牌发送通知。 and I deployed cloud functions it says successfully sent notifications with status ok.
我部署了云功能,它说成功发送状态为ok的通知。 But it doesn't displays in mobile device.
但它不会显示在移动设备中。 My Index.js is
我的Index.js是
'use strict';
const functions = require('firebase-functions');
const Firestore = require('@google-cloud/firestore');
const admin = require('firebase-admin');
const firestore = new Firestore();
const db = admin.firestore();
admin.initializeApp(functions.config().firebase);
exports.hellouser = functions.firestore
.document('users/{token}')
.onWrite(event =>{
var document = event.data.data();
console.log("tokens",document);
var token = ['cdNN0AbYKU0:APA91bEyL0zo3zwHZD8H43Vp7bxAfYgehlVI8LrKktPO2eGuByVDdioysIGxHe5wocwq8ynxRToJPpOve_M59YY_MIRbWLnF9AIgoTwJORXZbw6VBw7']// this is my FCM token.
if(
const payload = {
notification: {
title: "Message",
body: "hi hello",
sound: "default"
}
};
return admin.messaging().sendToDevice(token, payload).then((response)=> {
console.info("Successfully sent notification")
}).catch(function(error) {
console.warn("Error sending notification " , error)
});
});
How to send notifications based on the FCMtoken. 如何基于FCMtoken发送通知。
If it's the exact code you use then check syntax near if(
. This may help you. 如果您使用的是正确的代码,请检查
if(
附近的语法。这可能会对您有所帮助。
Next write some code to go through your response
object. 接下来,编写一些代码来遍历您的
response
对象。 Firebase may take your tokens and payload, process them and return 200 OK response but in the response you will have errors. Firebase可能会获取您的令牌和有效负载,对其进行处理并返回200 OK响应,但是在响应中您将遇到错误。
Response has general structure like this: 响应具有如下一般结构:
{ results: [ { //stuff related to one token },{ //stuff related to one token } ], canonicalRegistrationTokenCount: 0, failureCount: 1, successCount: 0, multicastId: SOME_LONG_NUMBER }
Take in mind that response.results
array has status of each message sent to token in the same order as tokens in your token
array. 请记住,
response.results
数组具有以与token
数组中的token
相同的顺序发送给令牌的每个消息的状态。
You can see all posible errors in Firebase Documentation . 您可以在Firebase文档中看到所有可能的错误。
If response.failureCount > 0
then no messages were sent and you should get corresponding error in response.results
. 如果
response.failureCount > 0
则没有消息发送,并且应该在response.results
得到相应的错误。
Also learn about options
variable. 也了解
options
变量。 options.priority
must be 'high'
to guarantee fast message delivery. options.priority
必须为'high'
以确保快速传递消息。
Maybe this will help. 也许这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.