簡體   English   中英

如何每天在特定時間發送本地通知?

[英]How to send local notification everyday on a specific time?

我想每天上午 10.11 向用戶發送通知(本地通知)。 我做了以下事情:

在 MainActivity.java 中,我有一個按鈕單擊偵聽器來觸發警報(不確定是否有必要)。 好的,按鈕有以下代碼:

public void notifyMattie(View view) {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 11);
        calendar.set(Calendar.SECOND,1);

        Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
        intent.setAction("MY_NOTIFICATION_MESSAGE");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

    }

我還有另一個 class 稱為:NotificationReceiver.java,它看起來像:

package coinchain.com.coinchain;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;

public class NotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent repeating_intent = new Intent(context,Invest.class);
        repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notification Title")
                .setContentText("Notification text")
                .setAutoCancel(true);

        if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")){
            notificationManager.notify(100,builder.build());
        }
    }
}

當我在真實設備上運行它時,我看不到任何通知。 我哪里錯了?

對於一個簡單的調度程序,我建議添加石英。 設置非常簡單。

文檔: http://www.quartz-scheduler.org/

小心,時間是 UTC(對於 CronSheduler)

pom.xml

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.2</version>
    </dependency>

作業.java

public class Job implements Job {
public void execute(JobExecutionContext jec) throws JobExecutionException {
    System.out.println("This is a quartz job!");
}}

Main.java 必須包含

JobDetail job = JobBuilder
                  .newJob(Job.class)
                  .withIdentity("SimpleJob")
                  .build();

Trigger trigger = TriggerBuilder
                       .newTrigger()
                       .withIdentity("SimpleJob")
                       .withSchedule(CronScheduleBuilder.cronSchedule("0 11 10 * * ?"))
                       .build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

暫無
暫無

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

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