Android的延迟通知通知、Android

2023-09-06 13:38:29 作者:尘&墨

我想创建一个使用Android的通知管理器一个通知,但诀窍是,我想要的通知显示30天以后。在我的code我这样做:

 意图notificationIntent =新的意图(这一点,MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(此,0,notificationIntent,0);
时长= System.currentTimeMillis的()+(30 * 24 * 3600 * 1000);
通知通知=新的通知(R.drawable.some_image,冠军的时候);
notification.setLatestEventInfo(getApplicationContext(),你迟到了,有些说明,contentIntent);
NotificationManager纳米=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFY_ATTEND_ID,通知);
 

不过,该通知仍然显示瞬间。从我读,在何时参数来通知构造函数只用于在状态的通知进行排序。反正是有在未来某一日期/时间作出通知显示在?提前致谢。

解决方案   

反正是有在未来某一日期/时间作出通知显示在?

没有。

由于Falmarri建议,您将需要处理这个自己,虽然我不同意他的做法。您将需要使用 AlarmManager 。不过,我怀疑 AlarmManager 工作30天工期,但你可以试试。您可能需要使用 AlarmManager 为每天/每周的任务,通过不同的报警来安排当天的/周的通知。您还需要重构这个名册报警上重新启动,因为他们得到消灭,因为Falmarri建议。

Android9.0 安卓9 微信消息通知延迟 不提示

I am trying to create a Notification using Android's Notification Manager, however, the trick is that I want the notification to show up 30 days in the future. In my code I'm doing this:

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);

However, the notification is still showing up instantaneously. From what I read, the "when" parameter to the Notification constructor is only used to sort the notifications in the StatusBar. Is there anyway to make the notification show up in at a future date/time? Thanks in advance.

解决方案

Is there anyway to make the notification show up in at a future date/time?

No.

As Falmarri suggests, you will need to handle this yourself, though I disagree with his approach. You will need to use AlarmManager. However, I am skeptical that AlarmManager will work for 30-day durations, though you can try it. You may need to use AlarmManager for a daily/weekly task to schedule that day's/week's notifications via separate alarms. You will also need to reconstitute this roster of alarms on a reboot, since they get wiped, as Falmarri suggests.