-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNotificationAlarmHandler.cs
More file actions
79 lines (69 loc) · 3.55 KB
/
NotificationAlarmHandler.cs
File metadata and controls
79 lines (69 loc) · 3.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
75
76
77
78
79
using System;
using Android.App;
using Android.Content;
using Android.OS;
using AndroidX.Core.App;
namespace Scheduler_Reminders.Droid {
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { NotificationHandler })]
public class NotificationAlarmHandler : BroadcastReceiver {
public const string NotificationHandler = "NotificationAlarmHandler";
public const string ReminderId = "ReminderId";
public const string RecurrenceIndex = "RecurrenceIndex";
public const string Subject = "Subject";
public const string Interval = "Interval";
static Intent GetLauncherActivity() {
var packageName = Application.Context.PackageName;
return Application.Context.PackageManager.GetLaunchIntentForPackage(packageName);
}
string CurrentPackageName => Application.Context.PackageName;
string ReminderChannelId => $"{CurrentPackageName}.reminders";
NotificationManager NotificationManager => (NotificationManager)Application.Context.
GetSystemService(Context.NotificationService);
public NotificationAlarmHandler() {
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
NotificationManager.CreateNotificationChannel(
new NotificationChannel(ReminderChannelId, "Reminders",
NotificationImportance.High));
}
public override void OnReceive(Context context, Intent intent) {
Guid reminderId = intent.GetReminderId();
if (reminderId == Guid.Empty)
return;
int notificationId = reminderId.GetHashCode();
Intent resultIntent = GetLauncherActivity().PutExtras(intent.Extras).
SetFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);
PendingIntent resultPendingIntent = PendingIntent.GetActivity(
context,
notificationId,
resultIntent,
PendingIntentFlags.UpdateCurrent);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(Application.Context);
builder.SetContentIntent(resultPendingIntent);
builder.SetDefaults((int)NotificationDefaults.All);
builder.SetContentTitle(intent.GetStringExtra(Subject));
builder.SetContentText(intent.GetStringExtra(Interval));
builder.SetSmallIcon(Resource.Mipmap.app_icon);
builder.SetChannelId(ReminderChannelId);
builder.SetPriority((int)NotificationPriority.High);
builder.SetAutoCancel(true);
builder.SetVisibility((int)NotificationVisibility.Public);
NotificationManager.Notify(notificationId, builder.Build());
}
}
public static class IntentExtensions {
public static Guid GetReminderId(this Intent intent) {
string guidString = intent.GetStringExtra(NotificationAlarmHandler.ReminderId);
try {
return Guid.Parse(guidString);
}
catch {
return Guid.Empty;
}
}
public static int GetRecurrenceIndex(this Intent intent) {
return intent.GetIntExtra(NotificationAlarmHandler.RecurrenceIndex, -1);
}
}
}