gpt4 book ai didi

android - Node JS FCM token 未向用户发送通知

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:23 25 4
gpt4 key购买 nike

我正在尝试根据用户 FCM token 发送通知,如果该特定用户的 firebase 数据库有任何更改,我已使用 firebase 函数成功发送通知。但目前,node.JS 功能不提供发送给用户的任何日志消息/通知。请帮助我解决这些问题。

//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref('/Notification/{receiver_id}/push_id/{job_id}').onWrite((data, context) => {

const receiver_id = context.params.receiver_id;
const job_id = context.params.job_id;
console.log('Start');
console.log('receiverID : ' + receiver_id);
console.log('jobID : ' + job_id);

const DeviceToken = admin.database().ref(`/User/${receiver_id}/fcmtoken`).once('value');

return DeviceToken.then(result =>
{
const token_id = result.val();
console.log(token_id);
const payload =
{
notification:
{
title: "New Job Request",
body: `JobID ` + job_id,
tag: collapseKey,
icon: "default",
color: '#18d821',
sound: 'default',
}
};

return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was a notification feature.');
return null;

})
.catch(function(error) {
console.log('Error sending message:', error);
});
});
});

它不显示任何日志消息或任何通知。

最佳答案

您使用了不正确的 promise 。当触发函数完成时,sendToDevice 可能会中止,因为它没有等待该 promise 。

exports.pushNotification = functions.database.ref('/Notification/{receiver_id}/push_id/{job_id}').onWrite((data, context) => {

const receiver_id = context.params.receiver_id;
const job_id = context.params.job_id;
console.log('Start');
console.log('receiverID : ' + receiver_id);
console.log('jobID : ' + job_id);

const DeviceToken = admin.database().ref(`/User/${receiver_id}/fcmtoken`).once('value');

return DeviceToken.then(result =>
{
const token_id = result.val();
console.log(token_id);
const payload =
{
notification:
{
title: "New Job Request",
body: `JobID ` + job_id,
tag: collapseKey,
icon: "default",
color: '#18d821',
sound: 'default',
}
};
return admin.messaging().sendToDevice(token_id, payload)
})
.then(response => {
console.log('This was a notification feature.');
return null;
})
.catch(function(error) {
console.log('Error sending message:', error);
});
});

关于android - Node JS FCM token 未向用户发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56562295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com