public void ScheduleNextEnable(Profile prof) { Context context = Application.Context; string name = prof.Name; bool[] days = prof.Days; TimeUnit[] startTimes = prof.StartTimes; // First check if any day of week should apply bool shouldApply = false; foreach (bool day in days) { shouldApply |= day; } if (!shouldApply) { return; } Intent intent = new Intent(context, typeof(EnableProfileReceiver)); intent.AddCategory(name); PendingIntent pending = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.CancelCurrent); long targetTime = GetTargetTime(prof, true); if (mAlarmManager == null) { mAlarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService); } mAlarmManager.SetExact(AlarmType.RtcWakeup, targetTime, pending); }
private void OnButtonDelayAlarmClicked(object sender, System.EventArgs e) { Toast toast = Toast.MakeText(c, "+10 seconds", ToastLength.Short); manager.SetExact(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 10000, pi); toast.Show(); }
/// <summary> /// Adds an alarm to the alarm manager including its reminder /// </summary> /// <param name="code">Alarm ID of alarm used as code</param> /// <param name="alarmTime">Time of alarm</param> /// <param name="daysFromNow">Number of days until alarm</param> private void addAlarm(int code, TimeSpan alarmTime, int daysFromNow) { //get the alarm to set Alarm alarmToSet = alarms[findAlarm(code)]; //call the AlarmReceiver activity when the alarm is triggered Intent intent = new Intent(this, typeof(AlarmReceiver)); intent.PutExtra("Username", username); intent.PutExtra("AlarmID", code); intent.PutExtra("AlarmName", alarmToSet.AlarmName); intent.PutExtra("AlarmTime", (alarmToSet.AlarmTime).ToString(@"hh\:mm")); intent.PutExtra("AlarmSound", alarmToSet.AlarmSound); PendingIntent pendingIntent = PendingIntent.GetActivity(this, code, intent, PendingIntentFlags.CancelCurrent); mgr = (AlarmManager)GetSystemService(AlarmService); //work out the alarm time in milliseconds and account for times that are the next day //get a calendar instance for the current day/time Calendar now = Calendar.GetInstance(Java.Util.TimeZone.Default); //get a calendar instance for today and set the required alarm hour and minute Calendar alarm = Calendar.GetInstance(Java.Util.TimeZone.Default); alarm.Set(CalendarField.HourOfDay, alarmTime.Hours); alarm.Set(CalendarField.Minute, alarmTime.Minutes); alarm.Set(CalendarField.Second, 0); long alarmMillis = alarm.TimeInMillis + (86400000L * daysFromNow); //if the alarm time is before now and no alarm days are set then add a day if (alarm.Before(now) && daysFromNow == 0 && alarmToSet.AlarmDays[0] == 0) { alarmMillis += 86400000L; } //set the alarm mgr.SetExact(AlarmType.RtcWakeup, alarmMillis, pendingIntent); //set the reminder for the alarm if set if (alarmToSet.AlarmReminder != 0) { Intent reminderIntent = new Intent(this, typeof(AlarmReceiver)); reminderIntent.PutExtra("Reminder", true); reminderIntent.PutExtra("AlarmID", code); reminderIntent.PutExtra("AlarmName", alarmToSet.AlarmName); reminderIntent.PutExtra("AlarmTime", (alarmToSet.AlarmTime).ToString(@"hh\:mm")); reminderIntent.PutExtra("AlarmSound", alarmToSet.AlarmSound); PendingIntent pendingIntentReminder = PendingIntent.GetActivity(this, code + 1000, reminderIntent, PendingIntentFlags.CancelCurrent); //set the reminder alarm in the alarm manager mgr.SetExact(AlarmType.RtcWakeup, alarmMillis - ((alarmToSet.AlarmReminder) * 60 * 1000), pendingIntentReminder); } //Display message informing user that the alarm has been set TimeSpan currentOffset = System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now); var time = TimeSpan.FromMilliseconds(alarmMillis); Toast.MakeText(this, "Alarm set for: " + (new DateTime(1970, 1, 1) + time + currentOffset).ToString(), ToastLength.Short).Show(); }
public void SetAlarm(Context Activity, long miliseconds, PendingIntent pendingIntent) { AlarmManager alarmManager = (AlarmManager)Activity.GetSystemService(Context.AlarmService); //Intent intent = new Intent(Activity, typeof(AlarmReceiver)); //intent.PutExtra("repeat", true); //PendingIntent pendingIntent = PendingIntent.GetBroadcast(Activity, /*id de la alarma que sea unico */0, intent, PendingIntentFlags.CancelCurrent); //alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, miliseconds, AlarmManager.IntervalDay, pendingIntent); //API19 NOt work https://stackoverflow.com/questions/27806336/alarmmanager-setinexactrepeating-not-working-in-android-4-1-2-works-on-android if (Build.VERSION.SdkInt < BuildVersionCodes.Kitkat) { alarmManager.Set(AlarmType.RtcWakeup, miliseconds, pendingIntent); } else if (BuildVersionCodes.Kitkat <= Build.VERSION.SdkInt && Build.VERSION.SdkInt < BuildVersionCodes.M) { alarmManager.SetExact(AlarmType.RtcWakeup, miliseconds, pendingIntent); } else if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, miliseconds, pendingIntent); } }
private void SaveButton_Click(object sender, System.EventArgs e) { var msg = saveMessage(); Android.Content.Intent alarmIntent = new Android.Content.Intent(this, typeof(SmsAlarmReciever)); alarmIntent.PutExtra("recieverPhoneNumber", msg.RecieverNumber); alarmIntent.PutExtra("recieverMessage", msg.MessageContent); alarmIntent.PutExtra("messageSendDate", msg.Date.ToString()); //var triggerTime = msg.Date.AddHours(1).Subtract(System.DateTime.UtcNow).TotalMilliseconds; //var x = System.Convert.ToInt64(triggerTime); //Java.Lang.JavaSystem.tim var timeDifference = System.Convert.ToInt64(msg.Date.Subtract(System.DateTime.Now).TotalMilliseconds); PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)GetSystemService(AlarmService); alarmManager.SetExact(AlarmType.RtcWakeup, Java.Lang.JavaSystem.CurrentTimeMillis() + timeDifference, pendingIntent); //showNotification(msg); var intent = new Android.Content.Intent(this, typeof(MessageList)); StartActivity(intent); }
public void ScheduleCallbackAlarm(PendingIntent callbackPendingIntent, string callbackId, DateTime callbackTime) { lock (_callbackIdPendingIntent) { // update pending intent associated with the callback id. we'll need the updated pending intent if/when // we which to unschedule the alarm. _callbackIdPendingIntent[callbackId] = callbackPendingIntent; AlarmManager alarmManager = _service.GetSystemService(global::Android.Content.Context.AlarmService) as AlarmManager; long delayMS = (long)(callbackTime - DateTime.Now).TotalMilliseconds; long callbackTimeMS = Java.Lang.JavaSystem.CurrentTimeMillis() + delayMS; // https://github.com/predictive-technology-laboratory/sensus/wiki/Backwards-Compatibility #if __ANDROID_23__ if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API level 23 added "while idle" option, making things even tighter. } else if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { alarmManager.SetExact(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API level 19 differentiated Set (loose) from SetExact (tight) } else #endif { alarmManager.Set(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API 1-18 treats Set as a tight alarm } } }
private void ScheduleCallbackAlarm(PendingIntent callbackPendingIntent, TimeSpan delay) { AlarmManager alarmManager = _service.GetSystemService(global::Android.Content.Context.AlarmService) as AlarmManager; // cancel the current alarm for this callback id. this deals with the situations where (1) sensus schedules callbacks // and is subsequently restarted, or (2) a probe is restarted after scheduling callbacks (e.g., script runner). in // these situations, the originally scheduled callbacks will remain in the alarm manager, and we'll begin piling up // additional, duplicate alarms. we need to be careful to avoid duplicate alarms, and this is how we manage it. alarmManager.Cancel(callbackPendingIntent); long callbackTimeMS = Java.Lang.JavaSystem.CurrentTimeMillis() + (long)delay.TotalMilliseconds; // https://github.com/predictive-technology-laboratory/sensus/wiki/Backwards-Compatibility #if __ANDROID_23__ if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API level 23 added "while idle" option, making things even tighter. } else if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { alarmManager.SetExact(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API level 19 differentiated Set (loose) from SetExact (tight) } else #endif { alarmManager.Set(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API 1-18 treats Set as a tight alarm } }
static public void AddAlarmEvent(int seconds = 60) { DateTime alarmtime = DateTime.Now.AddSeconds(seconds); Intent alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver)); PendingIntent pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, AlarmId++, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService); Java.Util.Calendar calendar = Java.Util.Calendar.Instance; calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); calendar.Set(alarmtime.Year, alarmtime.Month - 1, alarmtime.Day, alarmtime.Hour, alarmtime.Minute, alarmtime.Second); if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M) { if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { //API 19 이상 API 23미만 alarmManager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent); } else { //API 19미만 alarmManager.Set(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent); } } else { //API 23 이상 alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent); } }
private void SendOnClick(object sender, EventArgs eventArgs) { SmsManager _smsManager; _smsManager = SmsManager.Default; EditText boy = FindViewById <EditText>(Resource.Id.First); EditText girl = FindViewById <EditText>(Resource.Id.FirstG); EditText boyM = FindViewById <EditText>(Resource.Id.Middle); EditText girlM = FindViewById <EditText>(Resource.Id.MiddleG); if (boy.Text == "") { GenerateNames(); } //_smsManager.SendTextMessage("4197969464", null, "message", null, null); _smsManager.SendTextMessage("9375704370", null, "Boy Name: " + boy.Text + " " + boyM.Text + "\n" + "Girl Name: " + girl.Text + " " + girlM.Text, null, null); if (!reminderSet) { Intent alarmIntent = new Intent(this, typeof(AlarmReceiver)); PendingIntent pending = PendingIntent.GetBroadcast(Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); am = (AlarmManager)this.GetSystemService(AlarmService); DateTime currentTime = DateTime.Now; var triggerTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 6, 0, 0); var triggerSpan = TimeSpan.FromDays(1).Subtract(currentTime.Subtract(triggerTime)); am.SetExact(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + (long)triggerSpan.TotalMilliseconds, pending); double tmp = triggerSpan.TotalMilliseconds; LongToast("You will be notified in : " + ((int)tmp / 3600000).ToString() + "Hours " + (((int)tmp % 3600000) / 60000).ToString() + "Minutes"); reminderSet = true; } }
public void SetAlarmForNextReminder(Context context) { long?nextReminderMillisTimestamp = GetNextReminderMillisTimestamp(); if (nextReminderMillisTimestamp.HasValue && nextReminderMillisTimestamp.Value > 0) { // Alarm is started 5 seconds after notification time // So, we have a better guarantee that it will be notified long delay = 1000 * 5; Intent reminderAlarmReceiver = new Intent(context, Java.Lang.Class.FromType(typeof(ReminderAlarmReceiver))); PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, reminderAlarmReceiver, PendingIntentFlags.UpdateCurrent); long triggerAtMillis = nextReminderMillisTimestamp.Value + delay; AlarmManager alarmManager = context.GetSystemService(Context.AlarmService) as AlarmManager; if (alarmManager != null) { // Method SetExactAndAllowWhileIdle() is for Doze mode, introduced in Android 6.0 if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, triggerAtMillis, pendingIntent); } else { alarmManager.SetExact(AlarmType.RtcWakeup, triggerAtMillis, pendingIntent); } } } }
public override void OnReceive(Context context, Intent intent) { if (intent.Action.Equals("android.intent.action.BOOT_COMPLETED")) { FileManager fileManager = new FileManager(); Alarm[] alarms = fileManager.GetAlarms(); for (int i = 0; i < alarms.Length; i++) { if (alarms[i].Status) { Intent newIntent = new Intent(context, typeof(AlarmReceiver)); PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, alarms[i].ID, newIntent, 0); Calendar calendar = Calendar.GetInstance(Java.Util.TimeZone.Default); calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); calendar.Set(CalendarField.Year, alarms[i].Year); calendar.Set(CalendarField.Month, alarms[i].Month); calendar.Set(CalendarField.DayOfMonth, alarms[i].Day); calendar.Set(CalendarField.HourOfDay, int.Parse(alarms[i].Hour)); calendar.Set(CalendarField.Minute, int.Parse(alarms[i].Minute)); calendar.Set(CalendarField.Second, 0); AlarmManager manager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService); manager.SetExact(AlarmType.RtcWakeup, Java.Lang.JavaSystem.CurrentTimeMillis() + 5 * 1000, pendingIntent); } } } }
private void OnTripSaveComplete(bool success) { activity.sendGaEvent("ui_action", "save trip", "save trip", Convert.ToInt16(success)); if (success) { Intent alarmIntent = new Intent(activity, typeof(LocationAlarmReceiver)); PendingIntent pi = PendingIntent.GetBroadcast(activity.ApplicationContext, 0, alarmIntent, 0); DateTime dtNow = DateTime.Now.ToLocalTime(); DateTime dtStart = this.itinerary.GetStartDate().ToLocalTime(); TimeSpan diffTS = dtStart - dtNow; long ms = (long)diffTS.TotalMilliseconds; if (((int)Build.VERSION.SdkInt) >= 19) { mAlarmManager.SetExact(AlarmType.ElapsedRealtimeWakeup, ms, pi); } else { mAlarmManager.Set(AlarmType.ElapsedRealtimeWakeup, ms, pi); } view.OnSaveComplete(); activity.SetResult(Result.Ok); activity.Finish(); } else { view.OnSaveError(); activity.SetResult(Result.Canceled); } }
private void ScheduleCallbackAlarm(ScheduledCallback callback, PendingIntent callbackPendingIntent) { AlarmManager alarmManager = _service.GetSystemService(global::Android.Content.Context.AlarmService) as AlarmManager; // cancel the current alarm for this callback id. this deals with the situations where (1) sensus schedules callbacks // and is subsequently restarted, or (2) a probe is restarted after scheduling callbacks (e.g., script runner). in // these situations, the originally scheduled callbacks will remain in the alarm manager, and we'll begin piling up // additional, duplicate alarms. we need to be careful to avoid duplicate alarms, and this is how we manage it. alarmManager.Cancel(callbackPendingIntent); long callbackTimeMS = callback.NextExecution.Value.ToJavaCurrentTimeMillis(); // see the Backwards Compatibility article for more information #if __ANDROID_23__ if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API level 23 added "while idle" option, making things even tighter. } else if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) { alarmManager.SetExact(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API level 19 differentiated Set (loose) from SetExact (tight) } else #endif { alarmManager.Set(AlarmType.RtcWakeup, callbackTimeMS, callbackPendingIntent); // API 1-18 treats Set as a tight alarm } SensusServiceHelper.Get().Logger.Log("Alarm scheduled for callback " + callback.Id + " at " + callback.NextExecution.Value + ".", LoggingLevel.Normal, GetType()); }
public void TestNote(string title, string message) { // TODO add Intent for user tap, navigate to an Activity Context context = Android.App.Application.Context; var noteIntent = new Intent(context, typeof(LocalNoteService_droid)); // TODO look into why the string extra has to be a string literal noteIntent.PutExtra(NOTIFICATION_TITLE, "" + title); noteIntent.PutExtra(NOTIFICATION_MESSAGE, "" + message); var pendingIntent = PendingIntent.GetBroadcast(context, 0, noteIntent, 0); var now = DateTime.Now; var showTime = now.AddSeconds(30.0); //var calendar = Calendar.Instance; //calendar.Set(CalendarField.Mi) AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService); alarmManager.SetExact(AlarmType.ElapsedRealtimeWakeup, showTime.Millisecond, pendingIntent); // Enabling BootReceiver.cs to automatically restart when device is rebooted var bootReceiver = new ComponentName(context, Java.Lang.Class.FromType(typeof(BootReceiver)).Name); var packageManager = context.PackageManager; packageManager.SetComponentEnabledSetting(bootReceiver, ComponentEnabledState.Enabled, ComponentEnableOption.DontKillApp); }
public void SetAlarm(DateTime time) { Intent alarmIntent = new Intent(Android.App.Application.Context, typeof(MyReceiver)); //alarmIntent.PutExtra("message", message); //alarmIntent.PutExtra("title", title); PendingIntent pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService); // alarm after 5 second from now //var second = 5; //alarmManager.SetExact(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + second * 1000, pendingIntent); var dateTime = DateTime.Now; dateTime = dateTime.AddSeconds(10); //Calendar dayCalendar = Calendar.GetInstance(Java.Util.TimeZone.Default); Calendar dayCalendar = Calendar.Instance; dayCalendar.Set(CalendarField.Year, dateTime.Year); // Thang 1 = 0 dayCalendar.Set(CalendarField.Month, dateTime.Month - 1); dayCalendar.Set(CalendarField.DayOfMonth, dateTime.Day); dayCalendar.Set(CalendarField.HourOfDay, dateTime.Hour); dayCalendar.Set(CalendarField.Minute, dateTime.Minute); dayCalendar.Set(CalendarField.Second, dateTime.Second); alarmManager.SetExact(AlarmType.RtcWakeup, dayCalendar.TimeInMillis, pendingIntent); }
private async Task SetAlarm(Notification notification, int id, DateTime occurrenceDate, Intent notificationIntent) { var firingCal = Java.Util.Calendar.Instance; firingCal.Set(CalendarField.Year, occurrenceDate.Year); firingCal.Set(CalendarField.Month, occurrenceDate.Month - 1); firingCal.Set(CalendarField.DayOfMonth, occurrenceDate.Day); firingCal.Set(CalendarField.HourOfDay, occurrenceDate.Hour); firingCal.Set(CalendarField.Minute, occurrenceDate.Minute); firingCal.Set(CalendarField.Second, occurrenceDate.Second); var triggerTime = firingCal.TimeInMillis; // for test purposes only var dateFormat = new SimpleDateFormat("dd:MM:yy:HH:mm:ss"); var cal = dateFormat.Format(triggerTime); var notificationOccurrence = new NotificationOccurrence(id, occurrenceDate, triggerTime); await this.storage.SaveAsync(notificationOccurrence); notificationIntent.PutExtra(NotificationPublisher.NOTIFICATION_ID, notificationOccurrence.Id.Value); notificationIntent.PutExtra(NotificationPublisher.MEDICATION_ID, id); notificationIntent.PutExtra(NotificationPublisher.NOTIFICATION, notification); notificationIntent.PutExtra(NotificationPublisher.NOTIFICATION_FIRE_TIME, triggerTime); var requestId = DateTime.Now.Millisecond; PendingIntent pendingIntent = PendingIntent.GetBroadcast(this.ctx, requestId, notificationIntent, PendingIntentFlags.CancelCurrent); AlarmManager alarmManager = (AlarmManager)this.ctx.GetSystemService(Context.AlarmService); alarmManager.SetExact(AlarmType.RtcWakeup, triggerTime, pendingIntent); }
private void SetAlarm() { AlarmManager manager = (AlarmManager)GetSystemService(Android.Content.Context.AlarmService); Intent i; PendingIntent pi; i = new Intent(this, typeof(AlarmBroadcast)); pi = PendingIntent.GetBroadcast(this, 0, i, 0); manager.SetExact(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 15000, pi); }
public void SetAlarm(Alarm alarm) { var alarmIntent = new Intent(Forms.Context, typeof(AlarmReceiver)); alarmIntent.SetFlags(ActivityFlags.IncludeStoppedPackages); alarmIntent.PutExtra("id", alarm.Id); PendingIntent pendingIntent = PendingIntent.GetBroadcast(Forms.Context, GetAlarmId(alarm), alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService); var difference = alarm.Time.Subtract(DateTime.Now.ToLocalTime().TimeOfDay); var differenceAsMillis = difference.TotalMilliseconds; alarm.Time.Add(new TimeSpan(1, 0, 0)); alarmManager.SetExact(AlarmType.RtcWakeup, Java.Lang.JavaSystem.CurrentTimeMillis() + (long)differenceAsMillis, pendingIntent); }
public void SetAlarm(int hourOfDay, int minute, bool store = true) { if (store) { Settings.SetAlarmTime(hourOfDay, minute); Settings.IsAlarmActive = true; } var calendar = Calendar.Instance; calendar.Set(CalendarField.HourOfDay, hourOfDay); calendar.Set(CalendarField.Minute, minute); calendar.Set(CalendarField.Second, 0); _alarmManager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, _alarmPendingIntent); }
private void ScheduleNotification(Int64 whenDisplayNotificationInUtcUnixTime, String title, String message, Int32 scheduleId, DateTime lessonDate, Int32 requestCode) { Int64 currentUtcUnixTime = GetDateTimeInUtcUnixTime(DateTime.UtcNow); if (whenDisplayNotificationInUtcUnixTime <= currentUtcUnixTime) { return; } AlarmManager alarmManager = _context.GetSystemService(Context.AlarmService) as AlarmManager; Intent intent = new Intent(_context, typeof(LessonsRemindPublisher)); intent.PutExtra( LessonsRemindPublisher.IntentNotificationScheduleIdKey, scheduleId.ToString() ); intent.PutExtra( LessonsRemindPublisher.IntentNotificationLessonDateKey, lessonDate.Ticks.ToString() ); intent.PutExtra(LessonsRemindPublisher.IntentNotificationTitleKey, title); intent.PutExtra(LessonsRemindPublisher.IntentNotificationMessageKey, message); PendingIntent pendingIntent = PendingIntent.GetBroadcast( _context, requestCode, intent, PendingIntentFlags.UpdateCurrent ); Int64 timeInMilliseconds = whenDisplayNotificationInUtcUnixTime * 1000; if (Build.VERSION.SdkInt < BuildVersionCodes.M) { alarmManager.SetExact(AlarmType.RtcWakeup, timeInMilliseconds, pendingIntent); } else { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, timeInMilliseconds, pendingIntent); } }
void INotificationCenter.UpdateNotifications(IList <TriggeredReminder> reminders, int maxCount) { AlarmManager alarm = (AlarmManager)AAplication.Context.GetSystemService(Context.AlarmService); for (int i = 0; i < maxCount; i++) { if (i < reminders.Count) { TriggeredReminder reminder = reminders[i]; PendingIntent pendingIntent = PendingIntent.GetBroadcast(AAplication.Context, i, CreateIntent(reminder), PendingIntentFlags.UpdateCurrent); alarm.Cancel(pendingIntent); alarm.SetExact((int)AlarmType.RtcWakeup, ToNativeDate(reminder.AlertTime).Time, pendingIntent); } else { PendingIntent pendingIntent = PendingIntent.GetBroadcast(AAplication.Context, i, CreateIntent(), PendingIntentFlags.UpdateCurrent); alarm.Cancel(pendingIntent); } } }
public Models.TimeNotification ScheduleTimeNotification(TimeNotification timeNotification) { try { Intent intent = new Intent(Android.App.Application.Context, typeof(NotificationSender)); intent.PutExtra("ID", timeNotification.Id); intent.PutExtra("Title", timeNotification.Title); intent.PutExtra("Body", timeNotification.Body); intent.PutExtra("Type", "TimeNotification"); PendingIntent broadcast = PendingIntent.GetBroadcast(Android.App.Application.Context, timeNotification.Id, intent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(AlarmService); var date = timeNotification.Date; var time = timeNotification.Time; Calendar calendar = Calendar.Instance; calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); calendar.Set(CalendarField.Year, date.Year); calendar.Set(CalendarField.Month, date.Month - 1); calendar.Set(CalendarField.DayOfMonth, date.Day); calendar.Set(CalendarField.HourOfDay, time.Hours); calendar.Set(CalendarField.Minute, time.Minutes); calendar.Set(CalendarField.Second, 0); alarmManager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, broadcast); return(timeNotification); } catch (Exception) { return(null); } }
public void ScheduleAlarm(Alarm alarm) { long millis = Android.OS.SystemClock.ElapsedRealtime() + 1000; PendingIntent pendingIntent = CreatePendingIntent(Forms.Context, alarm); AlarmManager alarmManager = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService); if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M) { alarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, millis, pendingIntent); } else if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { alarmManager.SetExact(AlarmType.ElapsedRealtimeWakeup, millis, pendingIntent); } else { alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, millis, pendingIntent); } }
public void SetAlarm(Alarm alarm) { Intent newIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver)); newIntent.SetData(Android.Net.Uri.Parse(alarm.ID.ToString())); PendingIntent pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, alarm.ID, newIntent, 0); alarm.Intent = pendingIntent.ToString(); Calendar calendar = Calendar.GetInstance(Java.Util.TimeZone.Default); calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis(); calendar.Set(CalendarField.Year, alarm.Year); calendar.Set(CalendarField.Month, alarm.Month - 1); calendar.Set(CalendarField.DayOfMonth, alarm.Day); calendar.Set(CalendarField.HourOfDay, int.Parse(alarm.Hour)); calendar.Set(CalendarField.Minute, int.Parse(alarm.Minute)); calendar.Set(CalendarField.Second, 0); AlarmManager manager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService); manager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent); }
public void SetAlarm(long crashCueMillis, string cueText) { Context context = Android.App.Application.Context; Intent alarmIntent = new Intent(context, typeof(CrashAlarmReceiver)); alarmIntent.PutExtra("title", CueConstants.NotifTitle); alarmIntent.PutExtra("message", CueConstants.NotifMessage + cueText); alarmIntent.PutExtra("Id", CueConstants.UniqueId); bool alreadyExists = (PendingIntent.GetBroadcast(context, CueConstants.UniqueId, alarmIntent, PendingIntentFlags.NoCreate) != null); PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, CueConstants.UniqueId, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService); if (alreadyExists) { alarmManager.Cancel(pendingIntent); } if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M) { if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) { alarmManager.SetExact(AlarmType.RtcWakeup, crashCueMillis, pendingIntent); } else { alarmManager.Set(AlarmType.RtcWakeup, crashCueMillis, pendingIntent); } } else { alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, crashCueMillis, pendingIntent); } }
public void SetSpecificReminder(string title, string message, DateTime dateTime) { Context context = Android.App.Application.Context; var noteIntent = new Intent(context, typeof(LocalNoteService_droid)); // TODO look into why the string extra has to be a string literal noteIntent.PutExtra(NOTIFICATION_TITLE, "" + title); noteIntent.PutExtra(NOTIFICATION_MESSAGE, "" + message); var pendingIntent = PendingIntent.GetBroadcast(context, 0, noteIntent, 0); Calendar cal = Calendar.GetInstance(Java.Util.TimeZone.Default, Locale.Us); cal.Set(CalendarField.Date, dateTime.Day); cal.Set(CalendarField.HourOfDay, dateTime.Hour); cal.Set(CalendarField.Minute, dateTime.Minute); cal.Set(CalendarField.Second, dateTime.Second); AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService); // TODO use Set or SetExact here? //alarmManager.Set(AlarmType.Rtc, cal.TimeInMillis, pendingIntent); alarmManager.SetExact(AlarmType.Rtc, cal.TimeInMillis, pendingIntent); }
/// <summary> /// /// </summary> /// <param name="request"></param> protected virtual bool ShowLater(NotificationRequest request) { if (request.Schedule.NotifyTime is null || request.Schedule.NotifyTime <= DateTime.Now) // To be consistent with iOS, Do not Schedule notification if NotifyTime is earlier than DateTime.Now { return(false); } var dictionaryRequest = NotificationCenter.GetRequestSerialize(request); var intent = new Intent(Application.Context, typeof(ScheduledAlarmReceiver)); foreach (var item in dictionaryRequest) { intent.PutExtra(item.Key, item.Value); } var pendingIntent = PendingIntent.GetBroadcast( Application.Context, request.NotificationId, intent, PendingIntentFlags.UpdateCurrent ); var utcAlarmTimeInMillis = (request.Schedule.NotifyTime.Value.ToUniversalTime() - DateTime.UtcNow).TotalMilliseconds; var triggerTime = (long)utcAlarmTimeInMillis; if (request.Schedule.RepeatType != NotificationRepeat.No) { TimeSpan?repeatInterval = null; switch (request.Schedule.RepeatType) { case NotificationRepeat.Daily: // To be consistent with iOS, Schedule notification next day same time. repeatInterval = TimeSpan.FromDays(1); break; case NotificationRepeat.Weekly: // To be consistent with iOS, Schedule notification next week same day same time. repeatInterval = TimeSpan.FromDays(7); break; case NotificationRepeat.TimeInterval: if (request.Schedule.NotifyRepeatInterval.HasValue) { repeatInterval = request.Schedule.NotifyRepeatInterval.Value; } break; } if (repeatInterval == null) { return(true); } var intervalTime = (long)repeatInterval.Value.TotalMilliseconds; MyAlarmManager.SetInexactRepeating(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + triggerTime, intervalTime, pendingIntent); } else { if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { MyAlarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + triggerTime, pendingIntent); } else { MyAlarmManager.SetExact(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + triggerTime, pendingIntent); } } AddPreferencesNotificationId(PreferencesPendingIdListKey, request.NotificationId); return(true); }