【问题标题】:Firebase Function error and because of it Device to device Notification got StuckFirebase 功能错误,因此设备到设备通知卡住了
【发布时间】: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_idtoken_id。对于某些用户,token_id 很可能为空。日志输出会告诉你它是哪一个。
  • 好的。我发现了我的一个愚蠢的错误,并在更正后开始正常工作。感谢您的回复。
  • 你能告诉我错误是什么吗,因为我也面临着类似的问题。

标签: firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:

你没有在某些地方使用模板标签${}

还要确保在 Firebase 中为您发送通知的用户存储了一个有效的 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 StudioLapit 聊天应用 教程的人,我参考了这个答案以了解上述更改 @987654321 @

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多