计划通知的混乱外观



我正在开发计划通知应用程序,我试图只触发与 4 个警报相关的 4 个通知(Cal,Cal2,Cal3,Cal4(

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

我编写了 4 个通知的出现顺序,运行应用程序 6 次。

124

3 4 注意:显示 3 和 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());

引用自开发者网站,

若要设置通知以便可以更新,请通过调用 NotificationManager.notify(( 发出通知 ID。若要在发出此通知后对其进行更新,请更新或创建 NotificationCompat.Builder 对象,从中生成通知对象,然后使用之前使用的相同 ID 发出通知。如果以前的通知仍然可见,系统会从通知对象的内容更新它。如果以前的通知已被关闭,则会改为创建新通知。

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

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

问题出在 API 上,我正在使用 API 19

在 android 上测试我的应用程序,这个 API 没有得到set,它需要setExact,所以我做了if声明来检查是否使用 set如果 API 小于 19,setExact API 19 及更高版本。

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
}

最新更新