时间推送通知 java

time push notifications java

我最近收到了一个正在运行的应用程序的推送通知。唯一的问题是通知会立即到来。但我需要它在 10 分钟后出现。 下面是我的有效代码,我如何让它在特定时间后出现?

public void onClick(View v) {
            Intent i = new Intent(new Intent(Settings.this, Start.class));
            startActivity(i);

            Intent intent = new Intent();
            PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent, 0);
            Notification noti = new Notification.Builder(Settings.this)
                    .setTicker("TickerTitle")
                    .setContentTitle("Price-Tracker")
                    .setContentText("ContentText")
                    .setSmallIcon(R.mipmap.pw)
                    .setContentIntent(pIntent).getNotification();
            noti.flags = Notification.FLAG_AUTO_CANCEL;
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nm.notify(0, noti);

例如,我尝试将 nm.notify(0, noti); 更改为 nm.notify(10, noti);,但它不起作用

你必须创建一个新的Thread,设置休眠10分钟然后推送通知

public void onClick(View v) {
        Intent i = new Intent(Settings.this, Start.class);
        startActivity(i);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 10 minutes * 60000 milliseconds(1 minute)
                    Thread.sleep(10 * 60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Intent intent = new Intent();
                PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent, 0);
                Notification noti = new Notification.Builder(Settings.this)
                        .setTicker("TickerTitle")
                        .setContentTitle("Price-Tracker")
                        .setContentText("ContentText")
                        .setSmallIcon(R.mipmap.pw)
                        .setContentIntent(pIntent).getNotification();
                noti.flags = Notification.FLAG_AUTO_CANCEL;
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                nm.notify(0, noti);
            }
        }).start();
}

也将 nm.notify(0, noti); 更改为 nm.notify(10, noti); 没有做任何事情,因为 0 是将显示的通知,这是第一个

您可以使用 android.os.Handler 延迟执行作业。

long DELAY = 10 * 60 * 1000; // 10 minutes
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //the job you want to be executed after DELAY milliseconds
        }
    }, DELAY);

如果此回答对您有帮助,请点击“赞”按钮下方的复选标记。

添加关注

       new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            nm.notify(0, noti);
                        }
                    });

                }
       },600000);

10 分钟 = 600000 毫秒

使用 runOnUiThread 从另一个或工作线程

更新UI
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            nm.notify(0, noti);
                        }
                    });

不要使用 thread.sleep,这是一个糟糕的解决方案。睡眠正在阻塞你的线程,而你可以在等待时继续其他工作...... 最好使用延迟 post 的处理程序或 AlarmManager 来处理您的等待时间。