This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNotificationService.android.cs
162 lines (140 loc) · 5.9 KB
/
NotificationService.android.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using System.Xml.Serialization;
using Android.App;
using Android.Content;
using Android.Provider;
using Android.Support.V4.App;
using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
namespace Plugin.NotificationService
{
/// <summary>
/// Interface for NotificationService
/// </summary>
public class NotificationServiceImplementation : INotificationService
{
private static Context AppContext = Application.Context;
private long NotifyTimeInMilliseconds(DateTimeOffset notifyTime) => notifyTime.ToUnixTimeMilliseconds();
private Intent CreateScheduledAlarmHandlerIntent() => new Intent(AppContext, typeof(ScheduledAlarmHandler));
private AlarmManager GetAlarmManager() => AppContext.GetSystemService(Context.AlarmService) as AlarmManager;
public static NotificationManager GetNotificationManager() => NotificationManager.FromContext(AppContext);
public long[] VibratePattern { get; set; } = { 0, 200, 200, 200, 200 };
public void ShowNotification(string title, string subtitle, string body, int id, TimeSpan waitInterval)
{
var alarmManager = GetAlarmManager();
var initialIntent = CreateScheduledAlarmHandlerIntent();
var builder = new LocalNotification(title, body, Application.Context.Resources.GetIdentifier("icon", "mipmap", Application.Context.PackageName), (int)NotificationImportance.Max, subtitle, VibratePattern);
initialIntent.PutExtra(ScheduledAlarmHandler.AlarmNotificationKey, SerializeNotification(builder));
var pendingIntent = PendingIntent.GetBroadcast(context: AppContext,
requestCode: 0,
intent: initialIntent,
flags: PendingIntentFlags.CancelCurrent);
var triggerTime = NotifyTimeInMilliseconds(DateTimeOffset.Now.Add(waitInterval));
alarmManager.SetAndAllowWhileIdle(AlarmType.RtcWakeup, triggerTime, pendingIntent);
}
public void ShowNotification(string title, string subtitle, string body, int id, DateTimeOffset notificationTime)
{
ShowNotification(title, subtitle, body, id, notificationTime.Subtract(DateTimeOffset.Now));
}
public void ShowPresetNotification(Notification.Builder builder)
{
using (Intent resultIntent = new Intent(AppContext, typeof(Activity)))
{
PendingIntent resultPendingIntent;
var notificationManager = GetNotificationManager();
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
using (TaskStackBuilder stackBuilder = TaskStackBuilder.Create(AppContext))
{
stackBuilder.AddNextIntent(resultIntent);
resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
}
builder.SetContentIntent(resultPendingIntent);
notificationManager.Notify(1000, builder.Build());
}
}
public void CancelNotification(int id)
{
var pendingIntent = PendingIntent.GetBroadcast(AppContext, 0, CreateScheduledAlarmHandlerIntent(), PendingIntentFlags.CancelCurrent);
var alarmManager = GetAlarmManager();
var notificationManager = GetNotificationManager();
alarmManager.Cancel(pendingIntent);
notificationManager.Cancel(id);
}
private string SerializeNotification(LocalNotification builder)
{
var xmlSerializer = new XmlSerializer(builder.GetType());
using (var stringWriter = new StringWriter())
{
xmlSerializer.Serialize(stringWriter, builder);
return stringWriter.ToString();
}
}
}
[BroadcastReceiver(Enabled = true, Label = "NotificationService Broadcast Receiver")]
public class ScheduledAlarmHandler : BroadcastReceiver
{
private static Context AppContext = Application.Context;
public static NotificationManager GetNotificationManager() => NotificationManager.FromContext(AppContext);
public const string AlarmNotificationKey = "AlarmNotification";
public override void OnReceive(Context context, Intent intent)
{
var extra = intent.GetStringExtra(AlarmNotificationKey);
LocalNotification notification = DeserializeNotification(extra);
var builder = notification.GenerateNotification();
using (Intent resultIntent = new Intent(AppContext, typeof(Activity)))
{
PendingIntent resultPendingIntent;
var notificationManager = GetNotificationManager();
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
using (TaskStackBuilder stackBuilder = TaskStackBuilder.Create(AppContext))
{
stackBuilder.AddNextIntent(resultIntent);
resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
}
builder.SetContentIntent(resultPendingIntent);
notificationManager.Notify(1000, builder.Build());
}
}
private LocalNotification DeserializeNotification(string notificationString)
{
var xmlSerializer = new XmlSerializer(typeof(LocalNotification));
using (var stringReader = new StringReader(notificationString))
{
return (LocalNotification)xmlSerializer.Deserialize(stringReader);
}
}
}
public class LocalNotification
{
private static Context AppContext = Application.Context;
public string ContentTitle { get; set; }
public string ContentText { get; set; }
public int SmallIcon { get; set; }
public int Priority { get; set; }
public string SubText { get; set; }
public long[] VibratePattern { get; set; }
public LocalNotification()
{
}
public LocalNotification(string contentTitle, string contentText, int smallIcon, int priority, string subText, long[] vibratePattern)
{
ContentTitle = contentTitle;
ContentText = contentText;
SmallIcon = smallIcon;
Priority = priority;
SubText = subText;
VibratePattern = vibratePattern;
}
public NotificationCompat.Builder GenerateNotification()
{
return new NotificationCompat.Builder(AppContext, "1000")
.SetContentTitle(ContentTitle)
.SetContentText(ContentText)
.SetSmallIcon(SmallIcon)
.SetPriority(Priority)
.SetSubText(SubText)
.SetSound(Settings.System.DefaultNotificationUri)
.SetVibrate(VibratePattern);
}
}
}