简体   繁体   English

Firebase错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组

[英]Firebase Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array

i want to send users notification when they are being sent friend request using firebase cloud messaging, but when the request is sent it returns this error in firebase function log 我想在使用Firebase云消息传递给朋友的朋友请求时向用户发送通知,但是当发送请求时,它将在Firebase功能日志中返回此错误

Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array. 错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组。 at FirebaseMessagingError.Error (native) at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) 在FirebaseMessagingError.Error处(本机)在FirebaseMessagingError.FirebaseError处(作为构造函数)(/ user_code / node_modules / firebase-admin / lib / utils / error.js:39:28)

this is the java-script code i am using 这是我正在使用的Java脚本代码

 'use strict' const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); 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 for :', user_id); if (!change.after.val()){ return console.log("A Notification Has Been Deleted From The Database: ", notification_id) } const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value'); return deviceToken.then(result => { const token_id = result.val(); const payload = { notification: { title: "Friend Request", body: "You just got a new friend request", icon: "default" } }; return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{ console.log('this is the notification') }); }); }); 

It sounds like token_id is null or an empty string. 听起来token_idnull或空字符串。 Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user. 这很可能是因为/Notifications/${user_id}/${notification_id}在您的数据库中不存在,例如,当目标用户没有令牌时。

To prevent the error message, simply check if the snapshot exists before using its value: 为避免出现错误消息,只需在使用快照值之前检查快照是否存在:

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

return deviceToken.then(result => {
    if (!result.exists() || result.val() === "") return false;

    const token_id = result.val();

    const payload = {
        notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{ 
        console.log('this is the notification')
    });
});

after many wasted hours i got to discover what was wrong. 经过许多小时的浪费后,我发现了什么地方出了问题。 now the issue was that i was pointing to the wrong path. 现在的问题是我指向了错误的道路。 this line of code was the issue 这行代码就是问题

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

it was supposed to be this 应该是这个

const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Cloud Function错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组 - Firebase Cloud Function error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array ESLint 7.1.0 错误:“模式”必须是非空字符串或非空字符串数组 - ESLint 7.1.0 Error: 'patterns' must be a non-empty string or an array of non-empty strings 错误:“输入”过滤器需要非空数组 - Error: A non-empty array is required for 'in' filters 如何修复“未知错误状态:错误:uid必须是一个非空字符串,最多128个字符。”在Firebase函数中 - How To Fix “Unknown error status: Error: The uid must be a non-empty string with at most 128 characters.” in Firebase Functions 消息内容必须是非空字符串 MessageEmbed - Message Content Must Be A Non-Empty String MessageEmbed 错误:消息内容必须是非空字符串。 || if (typeof data;== 'string') throw new error(errorMessage); - Error: Message content must be a non-empty string. || if (typeof data !== 'string') throw new error(errorMessage); 如果它是第一个非空数组,则返回true - Return true if it's first non-empty array JS:仅针对非空和字符串值类型过滤数组 - JS: Filter array only for non-empty and type of string values 错误:RangeError [MESSAGE_CONTENT_TYPE]:消息内容必须是非空字符串 - Error : RangeError [MESSAGE_CONTENT_TYPE]: Message content must be a non-empty string 未捕获(承诺)错误:“args.method”必须是非空字符串 - Uncaught (in promise) Error: 'args.method' must be a non-empty string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM