私はfirebaseのクラウド機能を使用してユーザープッシュ通知を送信しています。私はJSをよく理解していませんが、通知ペイロードを通じてアプリのバッジ番号を自動インクリメントし、通知が受信されるたびに1ずつ増やしたいと考えています。これは今私が持っているものです。私はfirebaseのドキュメンテーションを読んだが、私は彼らが何を記述しているのか理解するのに十分なJSの理解があるとは思わない。受信したプッシュ通知ごとにアプリバッジ番号を増やす方法
exports.sendPushNotificationLikes = functions.database.ref('/friend-like-push-notifications/{userId}/{postId}/{likerId}').onWrite(event => {
const userUid = event.params.userId;
const postUid = event.params.postId;
const likerUid = event.params.likerId;
if (!event.data.val()) {
return;
}
// const likerProfile = admin.database().ref(`/users/${likerUid}/profile/`).once('value');
const getDeviceTokensPromise = admin.database().ref(`/users/${userUid}/fcmToken`).once('value');
// Get the follower profile.
const getLikerProfilePromise = admin.auth().getUser(likerUid);
return Promise.all([getDeviceTokensPromise, getLikerProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const user = results[1];
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
const payload = {
notification: {
title: 'New Like!',
body: '${user.username} liked your post!',
sound: 'default',
badge: += 1.toString()
}
};
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);
});
});
});任意の助けを事前に
おかげ
イムわからない私はあなたが何を意味するかを理解しますつまり、「 はまだ正しくラップされていませんか?しかし、これはユーザのユーザ名の代わりに私に "undefined"を与えてくれました。 – Chris
上記の '' 'body'''値の例では、一重引用符を使用しています。 '' '$ {}' ''はただの文字列として扱われます。文字列補間を機能させるには、バックティック(タブキーの上)を使用する必要があります。 ( 'vs ')IDEを使用している場合、' '' {{} '' 'の中の項目のハイライト表示も変更する必要があります。 [MDNドキュメントテンプレートリテラル](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) – DILP