How can I call BroadCastReceiver inside a service with local notification -Xamarin.Android

I am working on a signaling application that runs at a scheduled time, and I want it to work when users run the application or not (both).

Application value will notify like normal alarms

Now I come to the point where I wanted to call broadcastReceiver inside the Service so that it performs this action.

What I noticed:

1.When I use Broadcast , Alarm notification only comes when I open the application. 2. When I use Service , the Alarm comes only when it is outside the application (when it is not running) (here I put my code in OnStartCommand ()).

So, I came to the conclusion that to add both of them in order to make a scheduled notification when the application is open or closed. But I dont know how

Now the source code is below.

Below is my Service .

 namespace Diabetes.Droid.Resources { [Service] public class AppStickyService : Service { public override void OnCreate() { base.OnCreate(); System.Diagnostics.Debug.WriteLine("Sticky Service - Created"); } public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId) { return StartCommandResult.Sticky; } public override Android.OS.IBinder OnBind(Android.Content.Intent intent) { System.Diagnostics.Debug.WriteLine("Sticky Service - Binded"); return null; } public override void OnDestroy() { System.Diagnostics.Debug.WriteLine("Sticky Service - Destroyed"); base.OnDestroy(); } } } 

my Broadcast

 namespace Diabetes.Droid { [BroadcastReceiver] [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)] public class AlarmReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { var message = intent.GetStringExtra("message"); var title = intent.GetStringExtra("title"); //Show toast here //Toast.MakeText(context, "Hello it me ", ToastLength.Short).Show(); var extras = intent.Extras; if (extras != null && !extras.IsEmpty) { NotificationManager manager_ = context.GetSystemService(Context.NotificationService) as NotificationManager; var notificationId = extras.GetInt("NotificationIdKey", -1); if (notificationId != -1) { manager_.Cancel(notificationId); } } //Create intent for action 1 (TAKE) var actionIntent1 = new Intent(); actionIntent1.SetAction("ARCHIVE"); var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent); //Create intent for action 2 (REPLY) var actionIntent2 = new Intent(); actionIntent2.SetAction("REPLY"); var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent); Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName); var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent); var pending = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent); //seting an alarm MedicationDatabase db = new MedicationDatabase(); var alarm_list = db.GetAlarmList(); //Debug.WriteLine(" Time -- : "+ m.ToString()); // Instantiate the Big Text style: Notification.BigTextStyle textStyle = new Notification.BigTextStyle(); var builder = new Notification.Builder(context) .AddAction(Resource.Drawable.tick_notify, "ARCHIVE", pIntent1) .AddAction(Resource.Drawable.cancel_notify, "REPLY", pIntent2) .SetSmallIcon(Resource.Drawable.ic_launcher) .SetContentTitle("Diabetics Reminder") .SetDefaults(NotificationDefaults.Sound) .SetStyle(new Notification .BigTextStyle() .SetSummaryText("") .SetBigContentTitle(title) .BigText(message) ).SetDefaults(NotificationDefaults.All); builder.SetContentIntent(pending); var notification = builder.Build(); var manager = NotificationManager.FromContext(context); manager.Notify(10010, notification); } } [BroadcastReceiver] [IntentFilter(new string[] { "ARCHIVE", "REPLY" })] public class CustomActionReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { switch (intent.Action) { case "ARCHIVE": try { MedicationDatabase db = new MedicationDatabase(); db.addtracktaken("true"); Toast.MakeText(context, "DOSAGE TAKEN", ToastLength.Short).Show(); } catch (Exception e) { Debug.WriteLine(e.StackTrace); } break; case "REPLY": try { Toast.MakeText(context, "ARCHIVE", ToastLength.Short).Show(); MedicationDatabase db = new MedicationDatabase(); db.addtrackmissed("true"); Toast.MakeText(context, "DOSAGE MISSED", ToastLength.Short).Show(); } catch (Exception e) { Debug.WriteLine(e.StackTrace); } break; } var extras = intent.Extras; if (extras != null && !extras.IsEmpty) { NotificationManager manager = context.GetSystemService(Context.NotificationService) as NotificationManager; var notificationId = extras.GetInt("NotificationIdKey", -1); if (notificationId != -1) { manager.Cancel(notificationId); } } } } } 

Class mainapplication

This class is the one that initializes Alarm to get started.

 namespace Diabetes.Droid { [Application] public class MainApplication : Application { ISetAlarm alarmService; public static Context AppContext; public MainApplication() { } public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer) { } public override void OnCreate() { base.OnCreate(); AppContext = this.ApplicationContext; SetAlarm(12, 22, title, message); } public void SetAlarm(int hour, int minute, string title, string message) { AppContext.StartService(new Intent(AppContext, typeof(AppStickyService))); Intent myintent = new Intent(Android.App.Application.Context, typeof(AppStickyService)); myintent.PutExtra("message", message); myintent.PutExtra("title", title); //PendingIntent pendingintent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, myintent, PendingIntentFlags.UpdateCurrent); PendingIntent pintent = PendingIntent.GetService(AppContext, 0, myintent, 0); Java.Util.Date date = new Java.Util.Date(); Java.Util.Calendar cal = Java.Util.Calendar.Instance; cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); cal.Set(Java.Util.CalendarField.HourOfDay, hour); cal.Set(Java.Util.CalendarField.Minute, minute); cal.Set(Java.Util.CalendarField.Second, 0); AlarmManager alarmManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager; alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pintent); alarmManager.Cancel(pintent); } private Boolean isMyServiceRunning() { ActivityManager man_ = (ActivityManager)Android.App.Application.Context.GetSystemService(Context.ActivityService); foreach (Android.App.ActivityManager.RunningServiceInfo service in man_.GetRunningServices(int.MaxValue)) { if (typeof(AppStickyService).Name.Equals(service.Service.ClassName)) { return true; } } return false; } public void StartService() { AppContext.StartService(new Intent(AppContext, typeof(AppStickyService))); if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(AppStickyService)), 0); AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService); //alarm.Cancel(pintent); } } public static void StopService() { AppContext.StopService(new Intent(AppContext, typeof(AppStickyService))); if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(AppStickyService)), 0); AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService); alarm.Cancel(pintent); } } } } 

Now, how can I improve my local notification so that it is like a normal Alarm in Android mobile devices.

+1
c # service notifications xamarin.android broadcastreceiver
source share

No one has answered this question yet.

See similar questions:

41
Using Broadcast / Broadcast Receiver to Send Messages from Service to Activity
7
Schedule notification using the alarm manager in xamarin formats for Android
3
Android reassigns alarm when app is killed

or similar:

868
How to use reflection to call a generic method?
4
Android: BatteryLevel widget not updating
2
How to fix a problem with a service instance?
one
multiple alarm
one
Android AlarmManager (sometimes) provides additional notification
one
Failed to start Intent service {flg = 0x10000000 cmp = com.company.callrecorder / .CallStateListener}: not found
0
Android - cancel / replace calendar notification using special
0
Automatically restart Android app on crash
0
Service closed OnHandleIntent mqtt
-2
Android Notification with SQLite Service

All Articles