AlarmManager 未发送通知

AlarmManager not delivering notification

我正在尝试在特定日期安排一些本地通知,我正在使用 AlarmManager 来安排它们。不幸的是,它在未来的时间里不起作用。有什么想法吗?

这就是我计算应该触发警报的时间的方式

 private long computeDelay(int day){

    long currentSystemTime = System.currentTimeMillis();

    Calendar scheduleTime = Calendar.getInstance();

    // used to compute number of hours and mins
    int currentDay = scheduleTime.get(Calendar.DAY_OF_MONTH);
    scheduleTime.set(Calendar.DAY_OF_MONTH, currentDay + day);
    scheduleTime.set(Calendar.HOUR, 7);
    scheduleTime.set(Calendar.MINUTE, 0);
    scheduleTime.set(Calendar.SECOND, 0);
    scheduleTime.set(Calendar.AM_PM, Calendar.PM);

    return scheduleTime.getTimeInMillis();
}

这就是我安排闹钟的方式。

private void scheduleNotification(Notification notification, long time) {
    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");

    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}

上下文是应用程序(即 getApplication())。

非常感谢!

问题在于我如何获得 PendingIntent。

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

我多次调用闹钟调度方法,请求代码始终为 0,所以它只调度了最后一个闹钟。我现在给每个闹钟安排一个唯一的请求代码,它似乎工作正常。