【发布时间】:2018-05-25 23:29:39
【问题描述】:
我刚刚开始使用 Firebase Functions,最初一切正常,但现在我面临以下错误。我在下面提供错误和代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.sendNotification = functions.database.ref('/Notification/{user_id}/{notification_id}').onWrite(event =>{
const user_id = event.params.user_id;
const Notification = event.params.Notification;
console.log('We have a Notification to send to :', user_id);
if (!event.data.val()) {
return console.console.log('A Notify has been deleted from the database :', notification_id);
}
const devicetoken = admin.database().ref(`/Users/{user_id}/device_token`).once('value');
return devicetoken.then(result =>{
const token_id = result.val();
const payload = {
notification: {
title : "Follower Request",
body: "You've received a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response =>{
console.log('This was the notification Feature')
});
});
});
以下是我在 Firebase 函数中收到的错误。
错误:提供给 sendToDevice() 的注册令牌必须是非空字符串或非空数组。 在 FirebaseMessagingError.Error (本机) 在 FirebaseMessagingError.FirebaseError [作为构造函数] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) 在 FirebaseMessagingError.PrefixedFirebaseError [作为构造函数] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) 在新的 FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:207:16) 在 Messaging.validateRegistrationTokensType (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:589:19) 在 Messaging.sendToDevice (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:210:14) 在 devicetoken.then.result (/user_code/index.js:36:30) 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)
Image of firebase function for above error
Image for providing an idea of how I have stored device_token_id
任何帮助将不胜感激。
【问题讨论】:
-
用这条语句
const token_id = result.val()获取令牌后,添加console.log()语句输出user_id和token_id。对于某些用户,token_id很可能为空。日志输出会告诉你它是哪一个。 -
好的。我发现了我的一个愚蠢的错误,并在更正后开始正常工作。感谢您的回复。
-
你能告诉我错误是什么吗,因为我也面临着类似的问题。
标签: firebase firebase-cloud-messaging google-cloud-functions