2

https://xamarinhelp.com/xamarin-background-tasks/#comment-1296 .. am using this background worker. I want to show Local Notification Repeatedly on every day where my application state either running or not. Its not work for me.. Please help. SendNotification() is calling from main activity. The repeated Local notification is worked well where my application state is running. But its not worked where my application state is not running.

public async Task SendNotification()
 {
  Intent alarmIntent = new Intent(this, typeof(AlarmReceiverNew));
  alarmIntent.PutExtra(“message”, “message”);
  alarmIntent.PutExtra(“title”, “Welcome”);

  PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
  AlarmManager alarmManager = (AlarmManager)this.GetSystemService(Context.AlarmService);
  long futureInMillis = SystemClock.ElapsedRealtime() + 12 * 3600 * 1000;
  alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, futureInMillis, pendingIntent);
 }

[BroadcastReceiver]
public class AlarmReceiverNew : BroadcastReceiver
 {
    public async override void OnReceive(Context context, Intent intent)
     {
       Intent notIntent = new Intent(context, typeof(MainActivity));
       PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
       NotificationManagerCompat manager = NotificationManagerCompat.From(context);

      var wearableExtender = new NotificationCompat.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, 1));

       NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        .SetContentIntent(contentIntent)
        .SetSmallIcon(Resource.Drawable.icon).SetContentTitle(“title”)
        .SetContentText(“message”)
        .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
        .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
        .Extend(wearableExtender);

        Notification notification = builder.Build();

        manager.Notify(0, notification);
       }
     }

am trying Android - Running a background task every 15 minutes, even when application is not running solution also. but output where same.

Aswathi PS
  • 35
  • 5

1 Answers1

0

On Android, you can use AlarmManager if version<=19, use JobScheduler if version>20. Here is the demo on github.

Like this:

   public void startTask()
        {
            if (Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)
            {

                AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
                Intent intent = new Intent(this, typeof(RepeatingAlarm));
                PendingIntent sender = PendingIntent.GetBroadcast(this, 0, intent, 0);
                // Schedule the alarm!
                alarmManager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime(), 5 * 1000, sender);

            }
            else
            {
                JobScheduler scheduler = (JobScheduler)GetSystemService(Context.JobSchedulerService);

                if (isJobPollServiceOn())
                {

                }
                else
                {
                    JobInfo jobInfo = new JobInfo.Builder(1,
                        new ComponentName(this, Java.Lang.Class.FromType(typeof(JobSchedulerService))))
                            .SetPeriodic(1000*5)
                            .Build();
                    scheduler.Schedule(jobInfo);
                }
            }

        }

isJobPollServiceOn method:

   [TargetApi(Value = 20)]
    public bool isJobPollServiceOn()
    {
        JobScheduler scheduler = (JobScheduler)GetSystemService(Context.JobSchedulerService);

        bool hasBeenScheduled = false;
        var jobInfos = scheduler.AllPendingJobs;

        for (int i = 0; i < jobInfos.Count; i++)
        {
            if (jobInfos[i].Id == 1)
            {
                hasBeenScheduled = true;
                break;
            }

        }


        return hasBeenScheduled;
    }
Robbit
  • 4,300
  • 1
  • 13
  • 29
  • its not work for me . am just download the github project and debugging.Can u please integrate with local notification – Aswathi PS Apr 24 '18 at 10:58
  • am running withe Redmi Y1 Lite. android version is 7.1.2 N2G47H. – Aswathi PS Apr 24 '18 at 11:19
  • Hi, @AswathiPS, If your phone is 7.1, after running the demo, please check the log, you will see "222222222" every 5 seconds. – Robbit Apr 24 '18 at 13:12
  • Hi, @AswathiPS, I have updated my demo with notification. Please check it. – Robbit Apr 25 '18 at 02:10
  • Hi, @Joe LV - MSFT am running the demo with same device. and replacing the SetPeriodic on JobInfo to 1*1000 , but its not trigger JobSchedulerService on each 1 sec. JobInfo is shows that IntervelMillis =900000. so is this working in 15 min intervel ? – Aswathi PS Apr 25 '18 at 03:54
  • Hi @Joe LV - MSFT The solution is worked after 15 min. where my application is in running state. Then am cleared my recent history and waiting more than 15 min. its not working for me. (ie, am not getting the notification actually i want a solution for this.) – Aswathi PS Apr 25 '18 at 04:32
  • Hi @Joe LV - MSFT , Is the Jobscheduler and BroadcastReceiver working where app state is not running? – Aswathi PS Apr 25 '18 at 04:59
  • If you clear your app from recent history, it still can post a notification, I haven't got Redmi, I test it on my emulator, it is still working from I start it . – Robbit Apr 25 '18 at 05:26
  • LV -MSFT can you please check with any device. and let you know if its work or not ? – Aswathi PS Apr 25 '18 at 05:33
  • Yeah, I have got a device, Sony 7.1, from [here](https://stackoverflow.com/questions/48649065/job-scheduler-in-android-nougat-is-not-running), I change `1000*5` to `1000*60*15`, I can get notification, I need time to test it, and I will give you comment. BTW, what is your min version. – Robbit Apr 25 '18 at 05:42
  • My APP Min version : Android 4.0(API Level 14- Ice Cream Sandwich) – Aswathi PS Apr 25 '18 at 05:59
  • You can use [firebase-job-dispatcher](https://learn.microsoft.com/en-us/xamarin/android/platform/firebase-job-dispatcher) if your phone has Google Play Services. – Robbit Apr 25 '18 at 06:06
  • Hi, I have tested the firebase-job-dispatcher, it will work, but the fired time of the job can't be sure, please read [this](https://stackoverflow.com/questions/37720015/firebase-jobdispatcher-how-does-it-work-compared-to-previous-apis-jobschedule), it will help you someway. – Robbit Apr 25 '18 at 07:42
  • can you share the firebase-job-dispatcher demo – Aswathi PS Apr 25 '18 at 08:34
  • Hi Thanks @ Joe Lv - MSFT its work for me and tested with Android version 4.4.2 model no : ASUS_T00J , and found that "Force stop" is enabled on my apps settiings. Later I tested the same with Redmi.. but the Force Stop is disabled. so am tested again the ASUS_T00J by disabled force stop. Then the local notification is not worked . How can I enable the same ? – Aswathi PS Apr 25 '18 at 10:19