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);
});
});
}); 형식 변환 가정의
badge: += 1.toString()
주의 : 어떤 도움이 가정
임 확실하지 무슨 말인지 이해 따옴표, 즉 : " 아직 포장되지 않았습니까? 하지만 네, 이것은 사용자 이름 대신에 "undefined"를주었습니다. – Chris
위의'''body'' 값에 대한 예제에서 작은 따옴표를 사용하고 있습니다. '''{{}''''는 보통 문자열로 취급됩니다. 문자열 보간이 작동하려면 백 탭 (Tab 키 위)을 사용해야합니다. ('vs ') IDE를 사용한다면,'''{{}'''안에있는 물건의 강조 표시 역시 변경되어야합니다. [MDN docs 템플릿 리터럴] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) – DILP