LoginSignup
2
2

More than 5 years have passed since last update.

Notificationメモ

Posted at

複数の通知がある場合、どれをタップしても同じ内容が表示される

【事象】
1. 通知タップ後に起動する画面に表示されるデータをIntentにputExtraしてPendingIntentに設定する
2. 複数の通知が届いた状態で通知をタップして画面を起動すると、すべて一番最近届いた通知のデータになっている

【対応方法】

IntentにsetAction(System.currentTimeMillis().toString())をセットする

val intent = Intent(this, MainActivity::class.java).apply {
            putExtra(MainActivity.BUNDLE_MAIL, mail)
            action = System.currentTimeMillis().toString()    // ★これ重要
        }
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)

Extraのデータが違っていてもOSは同じものだと判断して、古い通知をすべて新しいPendingIntentに更新してしまうよう。
なので、OSに各通知が別なことを伝えるためにsetAction(System.currentTimeMillis().toString())をセットして、古い通知が上書きされないようにしている。

参考:PendingIntent works correctly for the first notification but incorrectly for the rest

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2