-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathLocalNotificationsImplementation.cs
More file actions
74 lines (66 loc) · 2.55 KB
/
LocalNotificationsImplementation.cs
File metadata and controls
74 lines (66 loc) · 2.55 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
using System;
using System.Linq;
using Foundation;
using Plugin.LocalNotifications.Abstractions;
namespace Plugin.LocalNotifications
{
/// <summary>
/// Local Notifications implementation for macOS
/// </summary>
public class LocalNotificationsImplementation : ILocalNotifications
{
/// <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)
{
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, DateTime.Now);
}
/// <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)
{
var notification = new NSUserNotification()
{
Title = title,
InformativeText = body,
Identifier = id.ToString(),
DeliveryDate = (NSDate)notifyTime
};
NSUserNotificationCenter.DefaultUserNotificationCenter.ScheduleNotification(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)
{
var scheduled = NSUserNotificationCenter.DefaultUserNotificationCenter.ScheduledNotifications.FirstOrDefault(x => x.Identifier == id.ToString());
var delivered = NSUserNotificationCenter.DefaultUserNotificationCenter.DeliveredNotifications.FirstOrDefault(x => x.Identifier == id.ToString());
if (scheduled != null)
{
NSUserNotificationCenter.DefaultUserNotificationCenter.RemoveScheduledNotification(scheduled);
}
if (delivered != null)
{
NSUserNotificationCenter.DefaultUserNotificationCenter.RemoveDeliveredNotification(delivered);
}
}
}
}