计划通知的凌乱外观

时间:2015-12-27 20:35:01

标签: android notifications alarmmanager

我正在处理预定的通知应用程序,我试图只发出4个与4个警报相关的通知(cal,cal2,cal3,cal4)

问题是,每次运行应用程序时,我都不会收到所有四个通知。

我写了4个通知的外观序列,运行应用程序6次。

124

4

3 4注意:我听到两个音调在显示3和4的情况下运行,它就像同时发出两个通知但只显示一个。

4

4

4

这是我的代码:

Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE,27);
        cal.set(Calendar.MONTH,11);
        cal.set(Calendar.YEAR,2015);
        cal.set(Calendar.HOUR_OF_DAY, 21);
        cal.set(Calendar.MINUTE,56);
        cal.set(Calendar.SECOND, 10);

        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DATE,27);
        cal2.set(Calendar.MONTH,11);
        cal2.set(Calendar.YEAR,2015);
        cal2.set(Calendar.HOUR_OF_DAY, 21);
        cal2.set(Calendar.MINUTE,56);
        cal2.set(Calendar.SECOND, 20);

        Calendar cal3 = Calendar.getInstance();
        cal3.set(Calendar.DATE,27);
        cal3.set(Calendar.MONTH, 11);
        cal3.set(Calendar.YEAR,2015);
        cal3.set(Calendar.HOUR_OF_DAY, 21);
        cal3.set(Calendar.MINUTE,56);
        cal3.set(Calendar.SECOND, 30);

        Calendar cal4 = Calendar.getInstance();
        cal4.set(Calendar.DATE,27);
        cal4.set(Calendar.MONTH, 11);
        cal4.set(Calendar.YEAR,2015);
        cal4.set(Calendar.HOUR_OF_DAY, 21);
        cal4.set(Calendar.MINUTE,56);
        cal4.set(Calendar.SECOND, 40);

        AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
      Intent alertIntent = new Intent(this, AlertReceiver.class);

        alertIntent.putExtra("Notification Key", 1);
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        alertIntent.putExtra("Notification Key", 2);
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        alertIntent.putExtra("Notification Key", 3);
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        alertIntent.putExtra("Notification Key", 4);
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

这是OnReceive代码:

public void onReceive(Context context, Intent intent) {


        Integer notificationId  = intent.getIntExtra("Notification Key", -1);
        switch(notificationId){
            case 1:
                createNotification(context, "title1", "event1", "event of today");
                break;
            case 2:
                createNotification(context, "title2", "event2", "event of today");
                break;
            case 3:
                createNotification(context, "title3", "event3", "event of today");
                break;
            case 4:
                createNotification(context, "title4", "event4", "event of today");
                break;
        }


           }

    public void createNotification(Context context, String msg, String msgText, String msgAlert) {
        PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.not)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText);
        //intent to fire when notification clicked on
        mBuilder.setContentIntent(notificIntent);
        //how the person will be notified
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        //cancel notification when clicked in the taskbar
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);


       mNotificationManager.notify(0, mBuilder.build());

2 个答案:

答案 0 :(得分:1)

从开发者网站引用,

  

要设置通知以便更新,请通过调用NotificationManager.notify()向通知ID发出通知。要在发出通知后更新此通知,请更新或创建NotificationCompat.Builder对象,从中构建Notification对象,并使用您之前使用的相同ID发出通知。如果先前的通知仍然可见,则系统会从Notification对象的内容中更新它。如果先前的通知已被取消,则会创建新的通知。

因此要么确保在收到通知之前阅读/删除通知。或者使用不同的ID创建每个通知。

此外,如果您阅读有关警报管理器的信息,他们会说从api 21(我认为),set()现在是一个不精确的调用。因此,您的警报可能不会立即被解雇。这样做的问题在于,由于待处理意图中的ID始终相同,因此警报管理器将删除/更新现有警报,然后才会在系统中发出新警报。

答案 1 :(得分:0)

问题出在API上,我正在使用API​​ 19在Android上测试我的应用程序,这个API没有得到set,它需要setExact,所以我发了if个声明检查API是否小于19时是set,API 19及以上是setExact

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= Build.VERSION_CODES.KITKAT){
    // Do something for Kitkat and above versions
} else{
    // do something for phones running an SDK before Kitkat
}