简体   繁体   中英

Notification on specific time repeated every day

I want the app to first check something at a specific time (7 AM), and then, if a condition is true, send a notification out, even if the app isn't active or even "only" running in the background.

This is the code for now (in the MainActivity.java ):

Intent intent_notification = new Intent(getApplicationContext() , NotificationClass.class);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent_notification, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);

I am not sure about the NotificationClass.class. How does it has to look like in general?

Thanks in advance.

NotificationClass.class will be Alarm BroadcastReceiver. Alarm service will invoke this receiver on scheduled time.

public class NotificationClass extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //check something at a scheduled time
        if(condition is true){ 
             sendNotification();
        }
    }
}

Declare NotificationClass.class in your manifest :

<receiver android:name=".NotificationClass"
          android:process=":remote" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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