0
我试图在添加新游戏时向“PLAYER 2”发送通知。 这里是我的代码:发送通知不起作用的云功能
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);
});
});
});
当它被执行,在火力控制台说
TypeError: Cannot convert undefined or null to object
一样,如果它是零,但FCM是存在的,我在做什么错?
谢谢你,这是解决,但现在它抛出错误:提供了无效的注册令牌。确保它符合客户端应用程序从FCM注册时收到的注册令牌为什么? –
您是否在'/ USERS/$ {player2uid}/fcm'或多个下存储了一个令牌? –
我只存储一个 –