Reputation: 19
I just wanna to fire local push notification at the specific time every day! I found in the documentation I can achieve this. Below code:
public class MyDailyNotMorning extends DailyJob {
public static final String TAG = "MyDailyJob";
public static void schedule() {
// schedule between 1 and 6 *PM*
DailyJob.schedule(builder, TimeUnit.HOURS.toMillis(1), TimeUnit.HOURS.toMillis(6));
}
@NonNull
@Override
protected DailyJobResult onRunDailyJob(Params params) {
PendingIntent pi = PendingIntent.getActivity(getContext(), 0,
new Intent(getContext(), MainActivity.class), 0);
Notification notification = new NotificationCompat.Builder(getContext())
.setContentTitle("Morning")
.setContentText("It's time to send a photo!")
.setAutoCancel(true)
.setContentIntent(pi)
.setSmallIcon(R.mipmap.ic_launcher)
.setShowWhen(true)
// .setColor(Color.RED)
.setLocalOnly(true)
.build();
NotificationManagerCompat.from(getContext())
.notify(new Random().nextInt(), notification);
Log.d("myTag", "onRunJob: notification setted daily success:morning");
return DailyJobResult.SUCCESS;
}
}
Say in this case notification receives at 1 a.m(or p.m), so the issue is what the second parameter of this method. Or how can I achieve "receive local notification" at 8 a.m (only 8 a.m. not p.m)? Please help me?!
Upvotes: 0
Views: 559
Reputation: 791
Notes from the docs :
// schedule between 1 and 6 AM
DailyJob.schedule(new JobRequest.Builder(TAG), TimeUnit.HOURS.toMillis(1),TimeUnit.HOURS.toMillis(6));
It's not P.M . If you try 18:00:00 it's P.M. It follows 24-Hour clock.
Code
It's my theoretical thinking (testing needed) , but you can try this below then comment if worked.
You set the DailyJob
between 08:00:00 and 08:00:05. That means a five-seconds-difference between start-time and end-time.
You can use TimeUnit.Seconds.toMillis(5)
, just like what you've used before.
public class Tasker extends DailyJob {
static final String TAG = "do_update_value_job_tag";
static void schedulePeriodic(Context context) {
Long startMs = TimeUnit.HOURS.toMillis(8);
Long endMs = TimeUnit.HOURS.toMillis(8) + TimeUnit.SECONDS.toMillis(5); //←←←
JobRequest.Builder requestByTag = new JobRequest.Builder(Tasker.TAG).setUpdateCurrent(true);
DailyJob.schedule(requestByTag, startMs, endMs);
}
@NonNull
@Override
protected DailyJobResult onRunDailyJob(@NonNull Params params) {
//do stuff
return DailyJobResult.SUCCESS;
}
@Override
protected void onCancel() {
//do stuff
super.onCancel();
}
}
Upvotes: 1