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是存在的,我在做什麼錯?

回答

2

我想,而不是這樣的:

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); 
}); 
}); 
}); 
+0

謝謝你,這是解決,但現在它拋出錯誤:提供了無效的註冊令牌。確保它符合客戶端應用程序從FCM註冊時收到的註冊令牌爲什麼? –

+0

您是否在'/ USERS/$ {player2uid}/fcm'或多個下存儲了一個令牌? –

+0

我只存儲一個 –