简体   繁体   English

在特定时间发送通知

[英]Send Notification at a specific time

I need to, for example, at 1PM to send a notification.例如,我需要在下午 1 点发送通知。 how can i do this and repeat it periodically.我怎样才能做到这一点并定期重复。

I need this code to be implemented with the "repeat code" :我需要使用“重复代码”来实现此代码:

Intent intent = new Intent();

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);

    Notification notification = new NotificationCompat.Builder(this)
            .setTicker("Ticker Title")
            .setSmallIcon("icon")
            .setContentTitle("Notification Content Title")
            .setContentText("Output")
            .setContentIntent(pi)
            .setAutoCancel(true)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
Intent myIntent = new Intent(this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 12*60*60*1000 , pendingIntent);

using the notify service and alarm manager you can do the required.使用通知服务和警报管理器,您可以执行所需的操作。

Use alarm manager class to start an intent on your desired time.使用警报管理器类在您想要的时间开始意图。

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 02);
calendar.set(Calendar.MINUTE, 00);

// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day
alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis()/1000,
        AlarmManager.INTERVAL_DAY, alarmIntent);

Here, you need to provide the time at which you want to call the intent.在这里,您需要提供要调用意图的时间。 In my code, it is 2AM every night.在我的代码中,它是每晚凌晨 2 点。 Also, for repeat interval, I selected a day because my process needed to be called on daily basis.此外,对于重复间隔,我选择了一天,因为我的流程需要每天调用。

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

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