-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNotificationCenter.cs
More file actions
97 lines (86 loc) · 4.17 KB
/
NotificationCenter.cs
File metadata and controls
97 lines (86 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Foundation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using UserNotifications;
using Xamarin.Forms;
using DevExpress.XamarinForms.Scheduler;
[assembly: Dependency(typeof(Scheduler_Reminders.iOS.NotificationCenter))]
namespace Scheduler_Reminders.iOS {
public class NotificationCenter : INotificationCenter {
public class ReminderIdentifier {
public Guid Guid { get; private set; }
public int RecurrenceIndex { get; private set; }
public ReminderIdentifier(Guid guid, int recurrenceIndex) {
Guid = guid;
RecurrenceIndex = recurrenceIndex;
}
}
public static string SerializeReminder(TriggeredReminder reminder) {
return reminder.Id + ":" + reminder.Appointment.RecurrenceIndex.ToString();
}
public static ReminderIdentifier DeserializeReminder(string reminderIdentifier) {
string[] splitData = reminderIdentifier.Split(':');
string recurrenceId = splitData[0];
int recurrenceIndex = Int32.Parse(splitData[1]);
Guid reminderGuid = Guid.Parse(recurrenceId);
return new ReminderIdentifier(reminderGuid, recurrenceIndex);
}
readonly ReminderNotificationCenterVersion notificationsCore;
public NotificationCenter() {
notificationsCore = new ReminderNotificationCenterVersion();
}
public void UpdateNotifications(IList<TriggeredReminder> reminders, int maxCount) {
notificationsCore.UpdateRemindersNotifications(reminders);
}
}
// Send notifications via the UNUserNotificationCenter system.
public class ReminderNotificationCenterVersion {
readonly UNUserNotificationCenter notificationCenter =
UNUserNotificationCenter.Current;
string CreateMessageContent(TriggeredReminder reminder) {
return reminder.Appointment.Interval.ToString("{0:g} - {1:g}", null);
}
Task<Tuple<bool, NSError>> RequestUserAccess() {
UNAuthorizationOptions options = UNAuthorizationOptions.Alert |
UNAuthorizationOptions.Sound |
UNAuthorizationOptions.Badge;
return notificationCenter.RequestAuthorizationAsync(options);
}
async void ScheduleReminderNotification(TriggeredReminder reminder, int badge) {
UNMutableNotificationContent content = new UNMutableNotificationContent() {
Title = reminder.Appointment.Subject,
Body = CreateMessageContent(reminder),
Sound = UNNotificationSound.Default,
Badge = badge,
};
NSDateComponents dateComponents = new NSDateComponents() {
Second = reminder.AlertTime.Second,
Minute = reminder.AlertTime.Minute,
Hour = reminder.AlertTime.Hour,
Day = reminder.AlertTime.Day,
Month = reminder.AlertTime.Month,
Year = reminder.AlertTime.Year,
TimeZone = NSTimeZone.SystemTimeZone,
};
UNCalendarNotificationTrigger trigger =
UNCalendarNotificationTrigger.CreateTrigger(dateComponents, false);
string identifier = NotificationCenter.SerializeReminder(reminder);
UNNotificationRequest request =
UNNotificationRequest.FromIdentifier(identifier, content, trigger);
await notificationCenter.AddNotificationRequestAsync(request);
}
public async void UpdateRemindersNotifications(IList<TriggeredReminder> featureReminders) {
Tuple<bool, NSError> authResult = await RequestUserAccess();
if (!authResult.Item1) {
Debug.WriteLine("User denied access to notifications");
return;
}
notificationCenter.RemoveAllPendingNotificationRequests();
for (int i = 0; i < featureReminders.Count; i++) {
ScheduleReminderNotification(featureReminders[i], i + 1);
}
}
}
}