1

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

所有通知都在不同的时间和日期出现,没有任何数据,但问题是,如果我为下午 3:27 和下午 3:28 设置了两个通知,那么第一个通知(下午 3:27)被取消(被第二个覆盖)第二是正常工作

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

此方法用于设置来自 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挂起的意图通知问题

同一活动的多个通知

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

4

3 回答 3

1

报价

发布要在状态栏中显示的通知。如果您的应用程序已经发布了具有相同 id 的通知并且尚未取消,它将被更新的信息替换。

但是你总是使用1参数id。发布多个通知时使用唯一 ID。

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

于 2013-02-22T10:48:05.440 回答
0

这可能会有所帮助

notif.flags |= Notification.FLAG_ONGOING_EVENT;

通知永远不会关闭...

于 2013-02-22T10:27:57.407 回答
0

尝试设置如下:

  contentIntent=PendingIntent.getActivity(p_context, i, new Intent(context, MenuScreen.class),PendingIntent.FLAG_CANCEL_CURRENT);

它可能会帮助你。

于 2013-02-22T10:35:14.870 回答