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);
Now, how can I improve my local notification so that it is like a normal Alarm in Android mobile devices.