我有一个警报接收器从广播中发出通知。完美运行。现在我添加了另一个通知,我喜欢独立发送2个通知。启动第一个通知,稍后启动第二个通知,第二个通知取消第一个通知,所以我只看到第二个。我需要在通知栏上有2个通知。同时删除“标记取消”也不会运行。
我的代码(第二次通知):
String ns2 = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager2 = (NotificationManager) context.getSystemService(ns2);
int icon = R.drawable.ic_launcherlight;
CharSequence tickerText = not1[x];
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "Casual";
CharSequence contentText = not1[x];
Intent notificationIntent = new Intent("com.example.myapp", null, context, NotifyCasual.class);
notificationIntent.putExtra("notify", not1[x]);
PendingIntent contentIntent = PendingIntent.getActivity(context, 2, notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String Sound = prefs.getString("sound", "");
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
final int HELLO2 = 1;
mNotificationManager2.notify(HELLO2, notification);
答案 0 :(得分:2)
你必须这样做。 在创建通知时,请按照此操作进行操作。
第一次通知:
mNotificationManager.notify(1, notifyDetails);
第二次通知:
mNotificationManager.notify(2, notifyDetails);
答案 1 :(得分:0)
使用setData.i使您的每个通知意图都是唯一的 当= Calendar.getInstance()。getTimeInMillis()
尝试以下代码
/*create intent for show notification details when user clicks notification*/
Intent intent =new Intent(getApplicationContext(), NotificationDetailsActivity.class);
intent.putExtra(NOTIFICATION_DATA, notificationData);
/*create unique this intent from other intent using setData */
intent.setData(Uri.parse("content://"+when));
/*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
/*get the system service that manage notification NotificationManager*/
NotificationManager notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
/*build the notification*/
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
getApplicationContext())
.setWhen(when)
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/*Create notification with builder*/
Notification notification=notificationBuilder.build();
/*sending notification to system.Here we use unique id (when)for making different each notification
* if we use same id,then first notification replace by the last notification*/
notificationManager.notify((int) when, notification);