如何在android中创建多个本地通知

时间:2015-11-23 10:31:54

标签: android notifications

在我的Android应用程序中,我需要在不同的时间间隔显示特定日期的多个本地通知,我使用警报管理器和广播接收器来做一个通知但是当我尝试实现多个通知时,只有第二个是显示。

这是我的MainActivity

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();
    cal.set(2015, 10, 23, 15, 03);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

这是BroadcastReceiver

Intent notificationIntent = new Intent(context, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent1 = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    Notification notification = builder.setContentTitle("TRR - 2016")
            .setContentText("Station - 1 closed grace period only 10min")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent1).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(mCounter, notification);

Notification notification1 = builder.setContentTitle("TRR - 2016")
            .setContentText("Station - 2 closed grace period only 10min")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent1).build();

    NotificationManager notificationManager1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager1.notify(++mCounter, notification1);

0 个答案:

没有答案