简体   繁体   English

时间推送通知Java

[英]time push notifications java

Ive recently got my push notifications for an app im working on to work. Ive最近收到了我正在工作的应用程序的推送通知。 The only thing is that the notification comes immediately. 唯一的是通知会立即发出。 But i need it to come after 10 minutes. 但是我需要10分钟后才能到。 Below is the code I have that works, how do I make it come after certain times? 以下是我可以使用的代码,如何使它在某些时候出现?

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

I tried changing the nm.notify(0, noti); 我试图更改nm.notify(0, noti); to nm.notify(10, noti); nm.notify(10, noti); for example but it did not work 例如,但它不起作用

Do no use thread.sleep, this is a horrible solution. 不要使用thread.sleep,这是一个可怕的解决方案。 Sleep is using blocking your thread while you could continue other job while waiting... Better use a Handler with post delayed or a AlarmManager to handle your waiting time. 睡眠正在阻塞线程,而您可以在等待时继续其他工作...最好使用延迟发布的Handler或AlarmManager处理等待时间。

you have to create a new Thread , set sleep for 10 minutes and then push the notification 您必须创建一个新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();
}

Also changing the nm.notify(0, noti); 同时更改nm.notify(0, noti); to nm.notify(10, noti); nm.notify(10, noti); didn't do anything because 0 is which notification will show, which is the first 没有执行任何操作,因为将显示0通知,这是第一个通知

You can use android.os.Handler for performing a job with a delay. 您可以使用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);

If this answer was helpful, please click the checkmark under the like button to indicate so. 如果此答案有帮助,请单击“喜欢”按钮下的复选标记以指示这样做。

Add following 添加以下

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

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

                }
       },600000);

10 min = 600000 millisecond 10分钟= 600000毫秒

Use runOnUiThread to update the UI from another or worker thread 使用runOnUiThread从另一个线程或辅助线程更新UI

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

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

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