locked
JobScheduler and FirebaseJobDispatcher don't work when the app is closed. RRS feed

  • Question

  • User383174 posted

    Hi. I have a JobService:

    [Service(Name = "com.mysite.MessagingJob")]
        [IntentFilter(new[] { FirebaseJobServiceIntent.Action })]
        public class MessagingJob : Firebase.JobDispatcher.JobService
        {
            const string tag = "my_job_tag";
            public override bool OnStartJob(IJobParameters jobParameters)
            {
                Task.Run(() =>
                {
                    LocalNotificationsImplementation.NotificationIconId = Resource.Drawable.Icon;
                    CrossLocalNotifications.Current.Show("notify", "You have a message!" + (new Random()).Next().ToString());
                });
    
                JobFinished(jobParameters, true);
                return true;
            }
            public override bool OnStopJob(IJobParameters jobParameters)
            {
                return true;
            }
        }
    

    and I start it in MainActivity.cs:

    Firebase.JobDispatcher.IDriver driver = new GooglePlayDriver(this);
                FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);
                JobTrigger myTrigger = Firebase.JobDispatcher.Trigger.ExecutionWindow(10, 30);
                Job myJob = dispatcher.NewJobBuilder()
                      .SetService<MessagingJob>("my_job_tag")
                      .SetLifetime(Lifetime.Forever)
                      .SetRecurring(true)
                      .SetTrigger(myTrigger)
                      .SetRetryStrategy(RetryStrategy.DefaultExponential)
                      .SetReplaceCurrent(true)
                      .Build();
                dispatcher.MustSchedule(myJob);
    

    The service runs periodically when the app is open or on the background but when the app is closed JobScheduler doesn't work.

    please see this if you have time

    I test it on Android 8.1 (Oreo)

    Sunday, October 13, 2019 8:27 AM

Answers

  • User383174 posted

    I got it with changing

    JobFinished(jobParameters, false);

    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
                Job myJob = dispatcher.NewJobBuilder()
                      .SetService<MessagingJob>("my_job_tag")
                      .SetLifetime(Lifetime.Forever)
                      .SetRecurring(true)
                      .SetTrigger(Firebase.JobDispatcher.Trigger.ExecutionWindow(10, 60))
                      .SetRetryStrategy(RetryStrategy.DefaultExponential)
                      .SetReplaceCurrent(true)
                      .Build();
                dispatcher.MustSchedule(myJob);
    

    this code runs every 1 minute. even the app is closed. (tested on Android 8.1) The JobScheduler is available on API 21 and higher.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, October 13, 2019 11:34 AM