简体   繁体   English

每天重复特定时间的通知

[英]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.我希望应用程序首先在特定时间(早上 7 点)检查某些内容,然后,如果条件为真,则发送通知,即使应用程序未处于活动状态甚至“仅”在后台运行。

This is the code for now (in the MainActivity.java ):这是现在的代码(在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.我不确定 NotificationClass.class。 How does it has to look like in general?一般情况下它应该是什么样子的?

Thanks in advance.提前致谢。

NotificationClass.class will be Alarm BroadcastReceiver. NotificationClass.class将是 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 :在您的清单中声明NotificationClass.class

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

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

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