java - 同一 Intent 的多个通知

标签 java android

我正在编写一个应用程序,在这个应用程序中我需要设置多个具有相同 Intent 的通知,就像每当用户点击任何通知时打开我的应用程序一样。

所有通知都在不同的时间和日期发出,没有任何数据,但问题是,如果我为 03:27 PM 和 03:28 PM 设置两个通知,那么第一个通知 (03:27 PM) 是已取消(被秒覆盖)并且秒正常工作

我目前正在使用此代码来实现此目标:

此方法用于设置来自 Activity 的通知:

 public static void setNotificationOnDateTime(Context context, long fireTime)
     {
         int requestID = (int) System.currentTimeMillis();

         AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, NotificationReceiver.class);

         PendingIntent pi = PendingIntent.getBroadcast(context, requestID, i, 0);
         am.set(AlarmManager.RTC_WAKEUP, fireTime, pi);
     }

我的 NotificationReceiver 类如下所示:

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {
     Log.w("TAG", "Notification fired...");

     nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

     PendingIntent contentIntent;

     Bundle bundle = intent.getExtras().getBundle("NotificationBundle");
     if(bundle == null)
     {
         contentIntent = PendingIntent.getActivity(context, 0,
                 new Intent(context, SplashScreen.class), 0);
     }

     else
     {
         contentIntent = PendingIntent.getActivity(context, 0,
                 new Intent(context, MenuScreen.class)
         .putExtra("NotificationBundle", bundle), 0);
     }

     Notification notif = new Notification(R.drawable.ic_launcher,
             "Crazy About Android...", System.currentTimeMillis());
     notif.setLatestEventInfo(context, "Me", "Message test", contentIntent);
     notif.flags |= Notification.FLAG_AUTO_CANCEL;
     nm.notify(1, notif);
 }

我已经花了很多时间在谷歌上搜索并找到了一些解决方案,但它们对我不起作用这里是其中一些的链接:

android pending intent notification problem

Multiple notifications to the same activity

如果有人知道如何做到这一点,请帮助我。

最佳答案

Quote :

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

但是您始终使用 1 作为 id 参数。发布多个通知时使用唯一 ID。

更新 如果这没有帮助,您仍然可以创建不比较为相等但具有相同效果的 Intent。

关于java - 同一 Intent 的多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15021858/

相关文章:

android - 放大 Android 渐变绘图的中心

android - WebView中的 "install Flash plugin"是什么意思?

java - 转到 Spring 4 后,我的 Autowiring 中断了

java - 在java中,我将如何进行计算,最大的数字减去较小的数字?

java - 如何在 ParseTreeMatch ANTLR4 上使用 getAll(...)

java - 使用 swagger codegen 插件通过多个 yaml 文件生成代码

android - 执行 Espresso 测试并在 EditText 为空时失败

javascript - 如何在 Android 版 Chrome 中获取正确的纵向视频

android - 使用 fragment 和 Activity 向上导航

javascript - jmeter webdriver无法点击登录弹出窗口