-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathLocalNotificationsImplementation.cs
More file actions
130 lines (117 loc) · 4.88 KB
/
LocalNotificationsImplementation.cs
File metadata and controls
130 lines (117 loc) · 4.88 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using Plugin.LocalNotifications.Abstractions;
using System.Linq;
using Foundation;
using UIKit;
using UserNotifications;
namespace Plugin.LocalNotifications
{
/// <summary>
/// Local Notifications implementation for iOS
/// </summary>
public class LocalNotificationsImplementation : ILocalNotifications
{
private const string NotificationKey = "LocalNotificationKey";
/// <summary>
/// Show a local notification
/// </summary>
/// <param name="title">Title of the notification</param>
/// <param name="body">Body or description of the notification</param>
/// <param name="id">Id of the notification</param>
public void Show(string title, string body, int id = 0)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(.1, false);
ShowUserNotification(title, body, id, trigger);
}
else
{
Show(title, body, id, DateTime.Now);
}
}
public void Show(string title, string body, int id = 0, string backgroundColor = null, string smallIcon = null, string largeIcon = null)
{
Show(title, body, id);
}
/// <summary>
/// Show a local notification at a specified time
/// </summary>
/// <param name="title">Title of the notification</param>
/// <param name="body">Body or description of the notification</param>
/// <param name="id">Id of the notification</param>
/// <param name="notifyTime">Time to show notification</param>
public void Show(string title, string body, int id, DateTime notifyTime)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
var trigger = UNCalendarNotificationTrigger.CreateTrigger(GetNSDateComponentsFromDateTime(notifyTime), false);
ShowUserNotification(title, body, id, trigger);
}
else
{
var notification = new UILocalNotification
{
FireDate = (NSDate)notifyTime,
AlertTitle = title,
AlertBody = body,
UserInfo = NSDictionary.FromObjectAndKey(NSObject.FromObject(id), NSObject.FromObject(NotificationKey))
};
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
}
public void Show(string title, string body, int id, DateTime notifyTime, string backgroundColor = null, string smallIcon = null, string largeIcon = null)
{
Show(title, body, id, notifyTime);
}
/// <summary>
/// Cancel a local notification
/// </summary>
/// <param name="id">Id of the notification to cancel</param>
public void Cancel(int id)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
UNUserNotificationCenter.Current.RemovePendingNotificationRequests(new string[] { id.ToString() });
UNUserNotificationCenter.Current.RemoveDeliveredNotifications(new string[] { id.ToString() });
}
else
{
var notifications = UIApplication.SharedApplication.ScheduledLocalNotifications;
var notification = notifications.Where(n => n.UserInfo.ContainsKey(NSObject.FromObject(NotificationKey)))
.FirstOrDefault(n => n.UserInfo[NotificationKey].Equals(NSObject.FromObject(id)));
if (notification != null)
{
UIApplication.SharedApplication.CancelLocalNotification(notification);
}
}
}
// Show local notifications using the UNUserNotificationCenter using a notification trigger (iOS 10+ only)
void ShowUserNotification(string title, string body, int id, UNNotificationTrigger trigger)
{
if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
return;
}
var content = new UNMutableNotificationContent()
{
Title = title,
Body = body
};
var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { });
}
NSDateComponents GetNSDateComponentsFromDateTime(DateTime dateTime)
{
return new NSDateComponents
{
Month = dateTime.Month,
Day = dateTime.Day,
Year = dateTime.Year,
Hour = dateTime.Hour,
Minute = dateTime.Minute,
Second = dateTime.Second
};
}
}
}