我试图在添加新游戏时试图将通知发送给"玩家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);
});
});
});
执行时,在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
}
}
}
。
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);
});
});
});