-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNotifierImplementation.cs
More file actions
156 lines (129 loc) · 5.92 KB
/
NotifierImplementation.cs
File metadata and controls
156 lines (129 loc) · 5.92 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
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
using System;
using System.IO;
using System.Xml.Serialization;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Support.V4.App;
using BaseTemplate.Droid.Services.LocalNotificationService;
using CoreServices.LocalNotificationsServices;
using Xamarin.Forms;
using AndroidApp = Android.App.Application;
using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
[assembly: Dependency(typeof(NotifierImplementation))]
namespace BaseTemplate.Droid.Services.LocalNotificationService
{
public class NotifierImplementation : ILocalNotificationService
{
private static NotificationManager NotificationManager =>
(NotificationManager)AndroidApp.Context.GetSystemService(Context.NotificationService);
private readonly string _channelId = $"{AndroidApp.Context.PackageName}.general";
private bool _channelInitialized;
public NotifierImplementation()
{
Initialize();
}
public event EventHandler NotificationReceived;
public void Cancel(int id)
{
Intent intent = CreateIntent(id);
PendingIntent pendingIntent =
PendingIntent.GetBroadcast(AndroidApp.Context, 0, intent, PendingIntentFlags.CancelCurrent);
AlarmManager alarmManager = GetAlarmManager();
alarmManager.Cancel(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(AndroidApp.Context);
notificationManager.Cancel(id);
}
public void Initialize()
{
if (!_channelInitialized) CreateNotificationChannel();
}
public void Notify(string title, string body, int id = 0)
{
if (!_channelInitialized) Initialize();
Intent resultIntent = GetLauncherActivity();
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(AndroidApp.Context);
stackBuilder.AddNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, _channelId)
.SetContentIntent(resultPendingIntent)
.SetContentTitle(title)
.SetContentText(body)
.SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources,
Resource.Drawable.notification_icon))
.SetSmallIcon(Resource.Drawable.notification_icon)
.SetDefaults((int)NotificationDefaults.All);
Notification notification = builder.Build();
NotificationManager.Notify(id, notification);
}
public void Notify(string title, string body, DateTime notificationDateTime, int id = 0)
{
Intent intent = CreateIntent(id);
LocalNotification localNotification = new LocalNotification
{
Title = title,
Body = body,
Id = id,
NotifyTime = notificationDateTime,
IconId = Resource.Drawable.notification_icon
};
string serializedNotification = SerializeNotification(localNotification);
intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, 0, intent, PendingIntentFlags.CancelCurrent);
long triggerTime = NotifyTimeInMilliseconds(notificationDateTime);
AlarmManager alarmManager = GetAlarmManager();
alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent);
}
public void ReceiveNotification(string title, string message)
{
NotificationEventArgs args = new NotificationEventArgs
{
Title = title,
Message = message
};
NotificationReceived?.Invoke(null, args);
}
private void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
const string channelName = "DefaultChannel";
NotificationChannel channel =
new NotificationChannel(_channelId, channelName, NotificationImportance.Default)
{
Description = "The default channel for notifications."
};
NotificationManager.CreateNotificationChannel(channel);
}
_channelInitialized = true;
}
private static Intent GetLauncherActivity()
{
return AndroidApp.Context.PackageManager.GetLaunchIntentForPackage(AndroidApp.Context.PackageName);
}
private static Intent CreateIntent(int id)
{
return new Intent(AndroidApp.Context, typeof(ScheduledAlarmHandler)).SetAction("LocalNotifierIntent" + id);
}
private static AlarmManager GetAlarmManager()
{
return AndroidApp.Context.GetSystemService(Context.AlarmService) as AlarmManager;
}
private static string SerializeNotification(LocalNotification notification)
{
XmlSerializer xmlSerializer = new XmlSerializer(notification.GetType());
using StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, notification);
return stringWriter.ToString();
}
private static long NotifyTimeInMilliseconds(DateTime notifyTime)
{
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(notifyTime);
double epochDifference = (new DateTime(1970, 1, 1) - DateTime.MinValue).TotalSeconds;
return utcTime.AddSeconds(-epochDifference).Ticks / 10000;
}
}
}