Get reminders based on data in a database

93 views Asked by At

I want to get reminders for some Appointments that I have saved in database and they have a notificationTime property witch is the time when a notification needs to be displayed.

My approach so far is to write some kind of job that runs 1 or 2 times a day to pull the notifications that need to be registered in the next 24h and ofc register them (if you guys have a better ideea lmk :D )

This works BUT:

  • Only if the app is in foreground / background; then I get notification every 15min or so;
  • If I KILL the app I don't receive notification on my physical device (Xiaomi Redmi Note 9 Pro with Android version 12 SKQ), only on the virtual one (Pixel 5 Android 13)

Right now I have a class that extends JobService and I use JobScheduler to schedule the Job to run every 15 min (for testing so I don't need to w8 12h xD )

Here is the JobScheduler witch I call in MainActivity file in OnCreate method

 Console.WriteLine("Schedualing job");
            TimeSpan interval = TimeSpan.FromMinutes(15);

            var javaClass = Java.Lang.Class.FromType(typeof(NotificationService));
            var componentName = new ComponentName(Application.Context, javaClass);

            var jobInfo = new JobInfo.Builder(1, componentName)
                .SetPeriodic(15 * 60 * 1000, 30 * 60 * 1000)
                .SetRequiredNetworkType(NetworkType.Any)
                .SetPersisted(true)
                .Build();
            
            var jobScheduler = (JobScheduler)GetSystemService(JobSchedulerService);
            var resultCode = jobScheduler.Schedule(jobInfo);

and here is the NotificationService.cs

[Service(Name = "com.companyname.deratizare_mobile.NotificationService",
        Permission = "android.permission.BIND_JOB_SERVICE")]
    public class NotificationService : JobService
    {
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            return StartCommandResult.StickyCompatibility;
        }

        public override bool OnStartJob(JobParameters @params)
        {
            Console.WriteLine("Job started");
            Task.Run(async () =>
            {
                //var hasSuccessful = await ProccessNotificationToRegister();
                var notification = new NotificationRequest
                {
                    Title = "Job",
                    Description = $"Description",
                    Schedule = new NotificationRequestSchedule
                    {
                        NotifyTime = DateTime.Now,
                    }
                };

                LocalNotificationCenter.Current.Show(notification);
                JobFinished(@params, false);
            Console.WriteLine("Job finished");
            });
            return true;
        }

        public override bool OnStopJob(JobParameters @params)
        {
            Console.WriteLine("Job stopped");
            return true;
        }
}

AndroidManifest

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.BIND_JOB_SERVICE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

UPDATE

I have given permission to auto start to my app from the device and it works

SOLUTION

I ended up using FCM and a hosted service on the server that checks the cache every 5 minutes where I have stored the next notification that needs to be displaied

1

There are 1 answers

4
Jessie Zhang -MSFT On

You can try to use Firebase push notifications.

With push notifications, you can update your users without requiring the app to run at all times or having it poll a server, potentially running down the battery.

For more information, you can check Implementing Push Notifications in Your Android Apps and Firebase Cloud Messaging.