Node.js 用于发送通知的云函数不工作
我正在尝试在添加新游戏时向“玩家2”发送通知。 这是我的密码:Node.js 用于发送通知的云函数不工作,node.js,firebase,firebase-realtime-database,firebase-cloud-messaging,google-cloud-functions,Node.js,Firebase,Firebase Realtime Database,Firebase Cloud Messaging,Google Cloud Functions,我正在尝试在添加新游戏时向“玩家2”发送通知。 这是我的密码: const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNewGameNotification= functions.database.ref('/GAMES/{gameId}/PLA
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNewGameNotification= functions.database.ref('/GAMES/{gameId}/PLAYER 2').onWrite(event => {
const player2uid = event.params.val;
const getDeviceTokensPromise = admin.database().ref(`/USERS/${player2uid}/fcm`).once('value');
return Promise.all([getDeviceTokensPromise]).then(results => {
const tokensSnapshot = results[0];
// Notification details.
const payload = {
'data': {
'title': "Tienes una nueva partida"
}
};
// Listing all tokens, error here below.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
执行时,firebase控制台中显示
TypeError: Cannot convert undefined or null to object
例如,如果它为空,但fcm存在,我做错了什么?我想不是这样:
const player2uid = event.params.val;
你想要这个:
const player2uid = event.data.val();
编辑:
此代码更新包含一些附加检查和简化。这对我有用
用于存储令牌的数据库结构至关重要。标记是键,而不是值。这些值不重要,可以是简单的占位符,例如布尔值
例如:
"USERS" : {
"Roberto" : {
"fcm" : {
"eBUDkvnsvtA:APA...rKe4T8n" : true
}
},
"Juan" : {
"fcm" : {
"fTY4wvnsvtA:APA91bGZMtLY6R...09yTLHdP-OqaxMA" : true
}
}
}
谢天谢地,问题解决了,但现在它给我带来了一个错误:提供的注册令牌无效。确保它与客户端应用程序从FCM注册收到的注册令牌匹配知道为什么吗?你是在
/USERS/${player2uid}/FCM
下存储一个令牌还是多个令牌?我只存储一个令牌。你是如何修复它的?哦,伙计,感谢你指出令牌必须作为密钥放入数据库中!你救了我几个小时的头撞在墙上。
exports.sendNewGameNotification= functions.database.ref('/GAMES/{gameId}/PLAYER 2').onWrite(event => {
const player2uid = event.data.val();
return admin.database().ref(`/USERS/${player2uid}`).once('value').then(snapshot => {
if (!snapshot.exists()) {
console.log('Player not found:', player2uid);
return;
}
const tokensSnapshot = snapshot.child('fcm');
if (!tokensSnapshot.exists()) {
console.log('No tokens for player: ', player2uid);
return;
}
// Notification details.
const payload = {
'data': {
'title': "Tienes una nueva partida"
}
};
// Listing all tokens, error here below.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});