firebase函数错误,由于IT设备,设备通知被卡住了



我刚刚掌握了Firebase功能,最初,一切都正确,但是现在我正面临以下错误。我在下面提供错误和我的代码。

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()的注册令牌(S)必须是非空字符串或非空数阵列。 在firbasemessagingerror.error(本地) 在firebasemessagingerror.firebaseerror [作为构造函数](/user_code/node_modules/firebase-admin/lib/lib/utils/error.js:39:28) 在firebasemessagingerror.prefixedfirebaseerror [作为构造函数](/user_code/node_modules/firebase-admin/lib/lib/utils/error.js:85:28) 在New Firebasemessagingerror(/USER_CODE/NODE_MODULES/FIREBASE-ADMIN/LIB/UTILS/ERROR.JS:207:16) 在Messing.validateregistrationTokenStype(/user_code/node_modules/firebase-admin/lib/messaging/messaging/messaging.js:589:19) 在Mesgaging.SendTodeVice(/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:210:14) 在devicetoken.then.result(/USER_CODE/index.js:36:30) 在process._tickdomaincallback(内部/process/next_tick.js:135:7)

上述错误函数的图像

图像提供了我如何存储device_token_id

的想法

任何帮助都将不胜感激。

您在某些地方没有使用templete标签 ${}

还要确保您要发送通知的用户中存储了有效的device_token

实施这些更改和更新方法后的最终代码将是:

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
  const user_id = context.params.user_id;
  const notification_id = context.params.notification_id;
  console.log('We have a notification to send to : ', context.params.user_id);
  if(!change.after.val()) {
      return console.log('A Notification has been deleted from the database : ' + context.params.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 => {
        return console.log('This was the notification Feature');  
    });
  });
});

致参考 tvac Studio lapit聊天应用程序的教程,我向上述答案提到了此答案。//stackoverflow.com/a/49746976/7549743

相关内容

  • 没有找到相关文章

最新更新