-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathINotifierImplementation.cs
More file actions
130 lines (116 loc) · 4.98 KB
/
INotifierImplementation.cs
File metadata and controls
130 lines (116 loc) · 4.98 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 System.Diagnostics;
using BaseTemplate.iOS.Services.LocalNotificationService;
using CoreServices.LocalNotificationsServices;
using Foundation;
using UserNotifications;
using Xamarin.Forms;
[assembly: Dependency(typeof(NotifierImplementation))]
namespace BaseTemplate.iOS.Services.LocalNotificationService
{
public class NotifierImplementation : ILocalNotificationService
{
private bool _hasNotificationPermissions;
public NotifierImplementation()
{
Initialize();
}
public void Initialize()
{
// Ask the user for permission to get notifications on iOS 10.0+
UNUserNotificationCenter.Current.RequestAuthorization(
UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(approved, error) => { _hasNotificationPermissions = approved; });
}
public event EventHandler NotificationReceived;
/// <summary>
/// Cancel a local notification
/// </summary>
/// <param name="id">Id of the notification to cancel</param>
public void Cancel(int id)
{
UNUserNotificationCenter.Current.RemovePendingNotificationRequests(new[] { CreateRequestIdForm(id) });
UNUserNotificationCenter.Current.RemoveDeliveredNotifications(new[] { CreateRequestIdForm(id) });
}
/// <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 Notify(string title, string body, int id = 0)
{
if (!_hasNotificationPermissions) return;
UNMutableNotificationContent content = new UNMutableNotificationContent
{
Title = title,
Body = body
};
UNTimeIntervalNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(.1, false);
BaseNotify(id, content, trigger);
}
/// <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>
/// <param name="notificationDateTime">Time to show notification</param>
public void Notify(string title, string body, DateTime notificationDateTime, int id = 0)
{
if (!_hasNotificationPermissions) return;
UNMutableNotificationContent content = new UNMutableNotificationContent
{
Title = title,
Body = body,
Sound = UNNotificationSound.Default
};
NSDateComponents dateComponent = new NSDateComponents
{
Month = notificationDateTime.Month,
Day = notificationDateTime.Day,
Year = notificationDateTime.Year,
Hour = notificationDateTime.Hour,
Minute = notificationDateTime.Minute,
Second = notificationDateTime.Second
};
UNCalendarNotificationTrigger trigger = UNCalendarNotificationTrigger.CreateTrigger(dateComponent, false);
BaseNotify(id, content, trigger);
}
public void ReceiveNotification(string title, string message)
{
NotificationEventArgs args = new NotificationEventArgs
{
Title = title,
Message = message
};
NotificationReceived?.Invoke(null, args);
}
/// <summary>
/// Code base logic of showing a notification
/// </summary>
/// <param name="content">the content of the notification</param>
/// <param name="trigger">the conditions that trigger the notification</param>
private static void BaseNotify(int id, UNMutableNotificationContent content, UNNotificationTrigger trigger)
{
UNNotificationRequest request =
UNNotificationRequest.FromIdentifier(CreateRequestIdForm(id), content, trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request, err =>
{
if (err != null)
{
Debug.WriteLine("Adding Notification Failed " + err.DebugDescription);
Debug.WriteLine("Adding Notification Failed " + err.LocalizedFailureReason);
}
});
var requests = UNUserNotificationCenter.Current.GetPendingNotificationRequestsAsync().Result;
}
/// <summary>
/// returns the correct notification request id form
/// </summary>
private static string CreateRequestIdForm(int id)
{
return $"Notification_{id}";
}
}
}