【发布时间】:2020-04-14 01:17:08
【问题描述】:
我没有在 Android 设备上收到通知。此错误出现在函数日志中。
ReferenceError:noticication_id 未定义 在exports.sendNotification.functions.database.ref.onWrite (/srv/index.js:28:84) 在 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) 在 /worker/worker.js:825:24 在 在 process._tickDomainCallback (internal/process/next_tick.js:229:7)
我在 Index.js 文件中的代码:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
console.log('A notification has been deleted :' , notification_id);
return null;
}
const sender_user_id = admin.database().ref(`/notifications/${receiver_user_id}/${noticication_id}`).once('value');
return sender_user_id.then(fromUserResult =>
{
const from_sender_user_id = fromUserResult.val().from;
console.log('you have a notification :',sender_user_id);
const userQuery = admin.database().ref(`/users/${receiver_user_id}/device_token`).once('value');
return userQuery.then(userResult =>
{
const senderUserName = userResult.val();
});
const DeviceToken = admin.database().ref(`/users/${receiver_user_id}/device_token`).once('value');
return DeviceToken.then(result =>
{
const token_id = result.val();
const payload =
{
notification:
{
from_sender_user_id : from_sender_user_id,
title: "New Chat Request",
body: `you have a new Chat Request from${senderUserName}`,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was a notification feature.');
});
});
});
});
【问题讨论】:
-
嗨,欢迎来到堆栈溢出。您调查了什么(查看跟踪)?您是否还确定错误包含“noticication_id”或者这是一个输入错误?
标签: javascript firebase google-cloud-functions