简体   繁体   English

如何使用云功能将推送通知发送到具有FCM令牌的多个设备

[英]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.

相关问题 如何使用 Firebase 云功能向设备发送通知? - How to send device to device notifications using Firebase cloud functions? 使用 Cloud Functions for Firebase 发送推送通知 - Send push notifications using Cloud Functions for Firebase FCM-通过设备令牌和环境/组发送android推送通知 - FCM - Sending android push notifications by device tokens and environment/group 如何使用 FCM 使用 php 脚本向多个设备发送推送通知? - How to send push notifications to multiple devices using php script using FCM? 如何在不使用Firebase控制台的情况下将fcm通知发送到单个设备 - how to send fcm notifications to a single device without using firebase console 通过云功能使用其UID向多个用户发送FCM通知 - Send FCM Notification to multiple users using their UID through cloud functions 是否可以使用云功能针对具有不同Firebase项目的2个不同应用发送FCM通知 - Is it possible to send the FCM notifications for 2 different apps with different firebase projects using cloud functions 如何在 NodeJS/Javascript 中的 FCM 中发送多个通知? - How to send multiple notifications in FCM in NodeJS/Javascript? 如何仅使用 Firebase 中的 FCM 令牌在不创建主题或组的情况下向 2 个设备发送推送通知? - How to send push notification to 2 devices without creating topic or group using just the FCM tokens in Firebase? 如何从 ADB 向设备发送 FCM(firebase 云消息传递)推送通知 - How to send FCM (firebase cloud messaging) push notification from ADB to device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM