私は一日中ソリューションを探していましたが、問題を解決できませんでした。以下のコードではデバイストークンは読み取れません。Firebase機能 - デバイストークンが空になった
以下にmy db構造体が含まれています。私はログを受け取ることができます:「あなたに新しいニュースがあります。私は新しいポストを追加しましたが、私は、ログを受信した「に送るべき通知トークンはありません。」それはものがすでに存在しているにもかかわらず、デバイストークンを検出できないことを意味し、どの。私は間違って何をしていますか?
{
"Newsv2" : {
"All" : {
"-Ktr7ZkuChCjsUIMb_4f" : {
"title" : "",
"type" : "",
}
},
"Usersv2" : {
"h0RzzpdO7nZVLpAR4fi7xRWUqsT2" : {
"device_token" : "",
"name" : "",
"user_no" : ""
}
},
}
/--News
--All
--name
--desc
/--Usersv2
--{userID}
--device_token
exports.sendNotif = functions.database.ref('/Newsv2/All/{newsID}').onWrite(event => {
const newsID = event.params.newsID;
const userID = event.params.userID;
if (!event.data.val()) {
return console.log('News! ', newsID);
}
console.log('We have a new News for you!',newsID);
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/Usersv2/${userid}/device_token`).once('value');
return Promise.all([getDeviceTokensPromise]).then(results => {
const tokensSnapshot = results[0];
//const follower = results[1];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
//console.log('Fetched follower profile', follower);
// Notification details.
const payload = {
notification: {
title: 'Test Message',
body: '',
icon: ''
}
};
// Listing all tokens.
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);
});
});
});
'getDeviceTokensPromise'を作成するコードを投稿してください。それはいつ実行されますか? –
申し訳ありませんが、私はそれを含めるのを忘れていました。//デバイス通知トークンのリストを取得します。 。 CONST getDeviceTokensPromise = admin.database()REF( '/ユーザ/ $ {followedUid}/notificationTokens').once( '値'); –
データベースに構造体がないか、コードで期待される値が含まれています。追加のヘルプを得るには、失敗したユーザーID(トークンなどのプライベートデータのプレースホルダー付き)に対して、エクスポートされたJSONを投稿する必要があります。 Firebaseコンソールからエクスポートを実行できます。 –