簡體   English   中英

在特定時間發送通知

[英]Send Notification at a specific time

例如,我需要在下午 1 點發送通知。 我怎樣才能做到這一點並定期重復。

我需要使用“重復代碼”來實現此代碼:

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

使用通知服務和警報管理器,您可以執行所需的操作。

使用警報管理器類在您想要的時間開始意圖。

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

在這里,您需要提供要調用意圖的時間。 在我的代碼中,它是每晚凌晨 2 點。 此外,對於重復間隔,我選擇了一天,因為我的流程需要每天調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM