Android延迟通知

Jef*_*eff 13 android

我正在尝试使用Android的通知管理器创建通知,但诀窍是我希望通知在将来30天内显示.在我的代码中,我这样做:

Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
long when = System.currentTimeMillis() + (30 * 24 * 3600 * 1000);
Notification notification = new Notification(R.drawable.some_image, "A title", when);
notification.setLatestEventInfo(getApplicationContext(), "You're late", "Some description", contentIntent);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFY_ATTEND_ID, notification);
Run Code Online (Sandbox Code Playgroud)

但是,通知仍然会立即显示.根据我的阅读,Notification构造函数的"when"参数仅用于对StatusBar中的通知进行排序.无论如何要在未来的日期/时间显示通知吗?提前致谢.

Com*_*are 4

有没有办法让通知在未来的日期/时间显示?

不。

正如法尔马里建议的那样,您需要自己处理这个问题,尽管我不同意他的方法。您将需要使用AlarmManager. AlarmManager不过,我对 30 天的效果持怀疑态度,尽管您可以尝试一下。您可能需要用于AlarmManager每日/每周任务,通过单独的闹钟安排当天/每周的通知。您还需要在重新启动时重新构建警报名册,因为它们会被擦除,正如法尔马里建议的那样。