简体   繁体   English

在奥利奥计划未来的通知

[英]Scheduling notifications for the future in Oreo

I'm using Android Oreo's new notification system. 我正在使用Android Oreo的新通知系统。 I want to schedule a notification in the future. 我想在将来安排通知。 I made a test program that is supposed to fire a notification 5 seconds later. 我制作了一个测试程序,应该在5秒钟后发出通知。 Instead, it displays the notification immediately. 而是,它立即显示通知。 Here is my code: 这是我的代码:

public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NotificationChannel notificationChannel = new NotificationChannel("default",
                "primary", NotificationManager.IMPORTANCE_DEFAULT);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(notificationChannel);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification notification = new Notification.Builder(getApplicationContext(), "default")
                        .setContentTitle("title")
                        .setContentText("body")
                        .setWhen(Calendar.getInstance().getTimeInMillis() + 5000)
                        .setSmallIcon(android.R.drawable.stat_notify_chat)
                        .setAutoCancel(true)
                        .build();
                manager.notify(123, notification);
            }
        });
    }
}

Thanks! 谢谢!

Use a Handler and its postDelayed method to give the notification in time intervals 使用Handler及其postDelayed方法按时间间隔发出通知

final Handler handler = new Handler()
handler.postDelayed( new Runnable() {

    @Override
    public void run() {
        //call your method here
        handler.postDelayed( this, 60 * 1000 );
    }
}, 60 * 1000 );

I'm afraid you've misunderstood what the setWhen function does. 恐怕您误解了setWhen函数的作用。 With this function you can alter the timestamp that is displayed in the notification. 使用此功能,您可以更改通知中显示的时间戳。 If you don't know what I'm talking about give it a timestamp of a few hours ago. 如果您不知道我在说什么,请给它几个小时前的时间戳。

If you want to schedule notifications in the future you'll have to schedule an alarm or something for the future and send the notification when the alarm is triggered. 如果您想将来安排通知,则必须安排警报或将来的通知,并在触发警报时发送通知。 I would recommend using android-job if you need this functionality in production today or the new WorkManager if you're just trying things out. 如果您今天需要在生产环境中使用此功能,我建议您使用android-job;如果您只是尝试尝试,则建议使用新的WorkManager Evernote has announced android-job will become deprecated some time in the future when WorkManager is out of alpha and stable. Evernote宣布,当WorkManager超出alpha版本且稳定时,android-job将在未来的某个时间过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM