Mihir Patel
Mihir Patel

Reputation: 486

Schedule notification at Monday of every week in Android

Want to schedule a notification which will fired at 11 AM of Every Monday of week. I am using firebase job dispatcher for this. Here is code snippet I have implemented but this is not working.

    Calendar currentDate = Calendar.getInstance();
    while (currentDate.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        currentDate.add(Calendar.DATE, 1);
    }
    currentDate.set(Calendar.HOUR_OF_DAY, 11);
    currentDate.set(Calendar.MINUTE, 0);
    currentDate.set(Calendar.SECOND, 0);
    currentDate.set(Calendar.MILLISECOND, 0);
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(SplashScreen.this));

    Job myJob = dispatcher.newJobBuilder()
            .setService(ScheduledNotificationService.class)
            .setTag(dispatcherTag)
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.executionWindow(Math.round(currentDate.getTime().getTime() / 1000), Math.round(currentDate.getTime().getTime() / 1000) + 60))
            .setReplaceCurrent(false)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .build();

    dispatcher.mustSchedule(myJob);

ScheduledNotificationService.class extends jobservice but onStartJob never gets called.

What can be wrong here?

Is there any better/correct approach apart from using firebase job dispatcher?

Upvotes: 2

Views: 1627

Answers (1)

Mihir Patel
Mihir Patel

Reputation: 486

FirebaseJobDispatcher didn't really helped, so I used AlarmManager instead and it is working like a charm. Here is how I achieved it,

    Calendar currentDate = Calendar.getInstance();
    while (currentDate.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        currentDate.add(Calendar.DATE, 1);
    }
    currentDate.set(Calendar.HOUR_OF_DAY, hour);
    currentDate.set(Calendar.MINUTE, minute);
    currentDate.set(Calendar.SECOND, 0);
    currentDate.set(Calendar.MILLISECOND, 0);

    Intent intent = new Intent(mContext, AlarmReceiver.class);
    intent.putExtra("extra info", "if needed");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, RequestCode, intent, 0);
    AlarmManager am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
    am.setRepeating(am.RTC_WAKEUP, currentDate.getTimeInMillis(), am.INTERVAL_DAY * 7, pendingIntent);

AlarmReceiver class to perform any action

 public class AlarmReceiver extends BroadcastReceiver {

    private Context mContext;

    @Override
    public void onReceive(Context context, Intent data) {
        mContext = context;
        //YOUR STUFF 
    }
}

This scheduling stops working after user reboots the device. Don't forget to set this again when reboot is completed. For this, add this in AndroidManifest and schedule alarm again.

    <receiver
        android:name=".AlarmManager.AlarmBootReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

Schedule alarms again in AlarmBootReceiver class.

Upvotes: 2

Related Questions