public Task ScheduleNotification(CoreNotification notification) { var task = new TaskFactory().StartNew(() => { var localNotification = new UILocalNotification(); localNotification.FireDate = NSDate.FromTimeIntervalSinceNow(60); //seconds localNotification.AlertBody = @"Your alert message"; localNotification.TimeZone = NSTimeZone.DefaultTimeZone; localNotification.RepeatInterval = NSCalendarUnit.Day; //localNotification.RepeatCalendar = UIApplication.SharedApplication.ScheduleLocalNotification(localNotification); }); return(task); }
/// <summary> /// Creates a single notification, an instance of <see cref="Android.App.Notification"/> class. /// </summary> /// <returns>Android notification created from <b>notification</b> parameter.</returns> /// <param name="notification"><b>CoreNotification</b></param> private Notification GetNotification(CoreNotification notification, DateTime occurrence, Intent notificationIntent) { var builder = new NotificationCompat.Builder(this.ctx); builder.SetContentTitle(notification.Title); builder.SetContentText(notification.Message + this.FormatOccurrence(occurrence)); builder.SetTicker("Ticker"); builder.SetSmallIcon(Resource.Drawable.Icon); builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Alarm)); builder.SetPriority((int)NotificationPriority.High); builder.SetVisibility((int)NotificationVisibility.Public); // visible on locked screen var action = this.GetAction(builder, notificationIntent, () => { System.Diagnostics.Debug.WriteLine("ACTION!"); }); builder.AddAction(action); return(builder.Build()); }
public Task ScheduleNotification(CoreNotification coreNotification) { var task = new TaskFactory().StartNew(() => { // todo return an identifier(s) for a notification(s) to store in db to be able to cancel them later // todo create notifications for all occrrences using AlarmManager.SetRepeating method Intent notificationIntent = new Intent(this.ctx, typeof(NotificationPublisher)); var notification = this.GetNotification(coreNotification, DateTime.Now.Date, notificationIntent); notification.Defaults |= NotificationDefaults.Lights | NotificationDefaults.Sound | NotificationDefaults.Vibrate; notificationIntent.PutExtra(NotificationPublisher.NOTIFICATION_ID, coreNotification.Id); // here we put long but NotificationPublisher expects int notificationIntent.PutExtra(NotificationPublisher.NOTIFICATION, notification); var requestId = DateTime.Now.Millisecond; PendingIntent pendingIntent = PendingIntent.GetBroadcast(this.ctx, requestId, notificationIntent, PendingIntentFlags.CancelCurrent); var firingCal = Calendar.Instance; var currentCal = Calendar.Instance; // todo set time according to occurrence firingCal.Set(CalendarField.HourOfDay, DateTime.Now.Hour); // At the hour you wanna fire firingCal.Set(CalendarField.Minute, DateTime.Now.Minute); // Particular minute firingCal.Set(CalendarField.Second, DateTime.Now.Second + 4); // particular second if (firingCal.CompareTo(currentCal) < 0) { firingCal.Add(CalendarField.DayOfMonth, 1); } var triggerTime = firingCal.TimeInMillis; // DateTime.Now.FromLocalToUnixTime(); AlarmManager alarmManager = (AlarmManager)this.ctx.GetSystemService(Context.AlarmService); alarmManager.SetRepeating(AlarmType.RtcWakeup, triggerTime, AlarmManager.IntervalFifteenMinutes /* or explicit value of millis, for example 10000*/, pendingIntent); // or //alarmManager.SetExact(); // or others }); return(task); }