0

私は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); 
    }); 
}); 

});任意の助けを事前に

おかげ

答えて

0

が、これは問題の行であると仮定すると:

badge: += 1.toString() 

型変換の前提条件を慎重に。 "1" + "1"を加えると "2"ではなく "11"になります。以下のような何かしようとしない理由:

badge: `${targetUser.notificationCount + 1}` 

をこれがnotificationCountは、スキーマ内の鍵であり、それは文字列として入力されていることを想定しています。新しい通知がで来るとき、それはインクリメントすることができますので、あなたはどこかにターゲットユーザの通知回数を永続化する必要があります。また、整数であることができ、その後、文字列の補間は不要である、すなわち:。

badge: targetUser.notificationCount + 1 

また、注意してくださいことここにあなたの文字列の補間はすなわち、バッククォートの代わりに、単一引用符でラップする必要があります:

body: `${user.username} liked your post!` 

私は相互作用がデータベースにマッピングされているかわかりません。この方法では、対象ユーザーの通知回数を維持して更新する必要があります。

+0

イムわからない私はあなたが何を意味するかを理解しますつまり、「 はまだ正しくラップされていませんか?しかし、これはユーザのユーザ名の代わりに私に "undefined"を与えてくれました。 – Chris

+0

上記の '' 'body'''値の例では、一重引用符を使用しています。 '' '$ {}' ''はただの文字列として扱われます。文字列補間を機能させるには、バックティック(タブキーの上)を使用する必要があります。 ( 'vs ')IDEを使用している場合、' '' {{} '' 'の中の項目のハイライト表示も変更する必要があります。 [MDNドキュメントテンプレートリテラル](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) – DILP

1

私は、これは問題が何であるかです推測している。また、上の

const payload = { 
    notification: { 
     title: 'New Like!', 
     body: `${user.username} liked your post!`, 
     sound: 'default', 
     badge: Number(notificationCount++) // => notificationCount + 1 
    } 
}; 

const payload = { 
    notification: { 
     title: 'New Like!', 
     body: '${user.username} liked your post!', 
     sound: 'default', 
     badge: += 1.toString() 
    } 
}; 

はあなたのスキーマで利用可能な通知回数プロパティがあるとはnotificationCountは、あなたがこれを行うことができると言いますこのbody: '${user.username} liked your post!'は、"user.username like your post!"として保存されます。これは何をやるべきことはこれで、あなたが望む動作ではありません:「また、ここではあなたの文字列の補間は、単一の代わりにバッククォートでラップされる必要があることに注意してください。

body: `${user.username} liked your post!` 
関連する問題