Get reminders based on data in a database

Bogdan Nicoara 0 Reputation points
2023-01-29T17:37:55.69+00:00

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 ) witch works but only if the app is in foreground / background and I get notification every 15min or so; but 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 virtuale 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;
        }
}

AndroidManifes

    <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" />
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,353 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,121 Reputation points Microsoft Vendor
    2023-01-30T05:15:20.7133333+00:00

    Hello,

    but only if the app is in foreground / background and I get notification every 15min or so; but if I kill the app I don't receive notification

    In Android devices, push notifications are mainly divided into two categories: online push and offline push.

    For online push, it requires the program to run in the background of the Android system to push successfully.

    Therefore, your need to use the foreground service or other means of keeping the program alive to receive notifications.

    If you want to receive notifications even after the program is terminated, you'll need to use the offline push method.

    This approach generally relies on the device vendor's unified push SDK and API, for Android devices, you can use FCM to implement this function, please refer to Firebase Cloud Messaging.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments