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.
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;
}