android - 每个通知都有自己的包含唯一数据的 PendingIntent

标签 android android-intent android-notifications

我有一个名为 sendNotification 的方法,可以多次调用。

private PendingIntent createNotificationIntent(Context context, String
type, int notificationId, String extra) {

   Intent notificationIntent = new Intent(this, ShimmyMobileActivity.class);
   notificationIntent.putExtra("com.****.****.action", type);
   notificationIntent.putExtra("com.****.****.notification_id",
notificationId);
   notificationIntent.putExtra("com.****.****.notification_extra",
extra);

   notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
         | Intent.FLAG_ACTIVITY_SINGLE_TOP);

   PendingIntent intent = PendingIntent.getActivity(this, 0,
         notificationIntent, PendingIntent.FLAG_IMMUTABLE);

   return intent;
}


private void sendNotification(int id, String message, String extra) {

   NotificationManager notificationManager = (NotificationManager) this
         .getSystemService(Context.NOTIFICATION_SERVICE);

   Notification.Builder builder = new
Notification.Builder(ShimmyMobileActivity.this);

   builder.setContentTitle("Shimmy Notification");
   builder.setContentText(message);
   builder.setSmallIcon(R.mipmap.ic_launcher);

   //builder.setDeleteIntent(this.createNotificationIntent(this,
"notification_delete", id, extra));
   builder.setContentIntent(this.createNotificationIntent(this,
"notification_clicked", id, extra));
   builder.setPriority(Notification.PRIORITY_MAX);

   if(this.preferences.getBoolean("vibrate", true)) {
      builder.setVibrate(new long[]{500, 500});
           Vibrator vibrator = (Vibrator) this
.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(500);
       }

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

      if (this.channel == null) {

         this.channel = new NotificationChannel(this.channelId,
"ShimmyMobile Notifications", NotificationManager.IMPORTANCE_DEFAULT);
         notificationManager.createNotificationChannel(this.channel);
      }

      builder.setChannelId(this.channelId);
   }

   Notification notification = builder.build();
   notificationManager.notify(id, notification);

}

当用户选择通知时 我会在 onResume 方法中获取与通知关联的数据。

我正在 onResume

中执行以下操作
if(action != null && action.equals("notification_clicked".toString())) {

         int notification_id =
this.getIntent().getIntExtra("com.****.****.notification_id",
-1);
         String extra = this.getIntent().getStringExtra("com.****.****.notification_extra");

}

问题是用户点击通知后,notification和notification_extra数据总是属于错误的通知。

我认为这是因为

PendingIntent Intent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

但我尝试了不同的标志,但没有成功。

我如何创建许多通知并附有它们自己的 PendingIntent

谢谢

最佳答案

我必须为每个通知发送一个唯一的整数到 PendingIntent。 上面我只是传递了 0

关于android - 每个通知都有自己的包含唯一数据的 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54949825/

相关文章:

android - 实现计时器以获取 gps 位置

android - 如何在工具栏中制作圆角菜单?

java - C# 服务器和 Android 客户端应用程序之间的 SSL 通信

android - 以编程方式从应用程序启动 Skype 并传递号码 - Android

android - Activity 循环以及如何限制它们

android - 更改通知的默认操作

android - 通知长按选项安卓

java - 如何在 xml 中绘制图像,但在 java 类中更改尺寸?

java - 如何在 android-java 中同时扩展 Activity 类和 BroadcastReceiver

android - 如何在我的 fcm 消息中使用 image 和 vibrate_timings