Javascript 如何为收到的每个推送通知增加应用程序徽章编号
我正在使用firebase云函数发送用户推送通知。我不太了解JS,但我希望能够通过通知有效负载自动增加应用程序徽章编号,并将收到的每个通知的编号增加1。这就是我现在拥有的。我已经阅读了firebase的文档,但我认为我没有足够的JS理解来理解他们在描述什么Javascript 如何为收到的每个推送通知增加应用程序徽章编号,javascript,apple-push-notifications,google-cloud-functions,Javascript,Apple Push Notifications,Google Cloud Functions,我正在使用firebase云函数发送用户推送通知。我不太了解JS,但我希望能够通过通知有效负载自动增加应用程序徽章编号,并将收到的每个通知的编号增加1。这就是我现在拥有的。我已经阅读了firebase的文档,但我认为我没有足够的JS理解来理解他们在描述什么 exports.sendPushNotificationLikes = functions.database.ref('/friend-like-push-notifications/{userId}/{postId}/{likerId}')
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);
});
});
}))
提前感谢您的帮助假设这是问题所在:
badge: += 1.toString()
小心类型转换假设。加上“1”+“1”将得到“11”,而不是“2”。为什么不试试这样的方法呢:
badge: `${targetUser.notificationCount + 1}`
这是假设notificationCount是模式中的一个键,并且它是以字符串形式键入的。您需要将目标用户的通知计数保留在某个位置,以便在收到新通知时增加该计数。它也可以是整数,因此不需要字符串插值,即:
badge: targetUser.notificationCount + 1
另外,请注意,此处的字符串插值需要用反勾号而不是单引号进行包装,即:
body: `${user.username} liked your post!`
我不知道这些交互是如何映射到您的数据库中的。这种方法需要持久化和更新目标用户的通知计数 我猜这就是问题所在:
const payload = {
notification: {
title: 'New Like!',
body: '${user.username} liked your post!',
sound: 'default',
badge: += 1.toString()
}
};
假设您的模式中有一个可用的通知计数属性,例如notificationCount
,那么您可以执行以下操作:
const payload = {
notification: {
title: 'New Like!',
body: `${user.username} liked your post!`,
sound: 'default',
badge: Number(notificationCount++) // => notificationCount + 1
}
};
同样在这个body:“${user.username}喜欢你的帖子!”
,这将另存为“user.username like your post!”
。这不是你想要的行为,你应该做的是:
body: `${user.username} liked your post!`
我不确定我是否理解你的意思:“另外,请注意,这里的字符串插值需要用反勾号而不是单引号进行包装,即:”“它们是否已经正确包装?但是是的,这给了我“undefined”,而不是您上面示例中使用单引号的
body
值的用户用户名。${}
将被视为普通字符串。您需要使用反勾号(tab键上方)进行字符串插值。(`vs')如果您使用的是IDE,${}
中的内容的突出显示也应该改变。