Android Multiple Notification sending same data on clicking

前端 未结 2 1055
太阳男子
太阳男子 2021-01-18 01:08

Notification in android taking same intent on clicking. I am sending notifications after installing the theme. Consider I install 4 themes and 4 notifications appear in Noti

相关标签:
2条回答
  • 2021-01-18 01:18

    Used this code which worked for me.

          int requestCode = new Random().nextInt();
          PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, 
          notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    
    0 讨论(0)
  • 2021-01-18 01:34

    I had the same issue, and the problem is that Android is being a little too smart and giving you the same PendingIntents instead of new ones. From the docs:

    A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

    Modify your code as follows to supply a unique requestCode:

    // ...
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, packageName.hashCode(), intent, 0);
    // ...
    

    This will ensure that a unique PendingIntent is used, as opposed to the same one.

    Note that hashCode() may not be unique, so if possible use another unique integer as the requestCode.

    0 讨论(0)
提交回复
热议问题