2

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mihir Patel
  • 456
  • 7
  • 20
  • I assume this code snippet of yours is being called either in the SplashScreen or MainActivity? And have you registered the service in the Manifest. – yUdoDis Jul 05 '18 at 17:16
  • it is called in SplasScreen but making setReplaceCurent(false) will not make any trouble, I suppose. And yes, ScheduledNotificationService.class is registered in AndroidManifest file. – Mihir Patel Jul 05 '18 at 18:02
  • You could just try testing it to run every 30 seconds instead of a Monday 11:00 AM, just to see if it works. Check your android logs in the logcat when your splash screen opens too. What does it say. – yUdoDis Jul 05 '18 at 19:14
  • I tried, but service is not getting invoked. I suspect, .setTrigger(Trigger.executionWindow(Math.round(currentDate.getTime().getTime() / 1000), Math.round(currentDate.getTime().getTime() / 1000) + 60)) is incorrect in someway, not sure what exactly. – Mihir Patel Jul 06 '18 at 04:44

1 Answers1

2

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.

Mihir Patel
  • 456
  • 7
  • 20