-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNotificationCenter.cs
More file actions
67 lines (63 loc) · 3.14 KB
/
NotificationCenter.cs
File metadata and controls
67 lines (63 loc) · 3.14 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
using Android.App;
using Android.Content;
using AndroidX.Core.App;
using Java.Sql;
using Scheduler_Reminders.Droid;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using AAplication = Android.App.Application;
using DevExpress.XamarinForms.Scheduler;
[assembly: Dependency(typeof(NotificationCenter))]
namespace Scheduler_Reminders.Droid {
public class NotificationCenter : INotificationCenter {
static Date ToNativeDate(DateTime dateTime) {
long dateTimeUtcAsMilliseconds =
(long)dateTime.ToUniversalTime().
Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
return new Date(dateTimeUtcAsMilliseconds);
}
void INotificationCenter.UpdateNotifications(IList<TriggeredReminder> reminders,
int maxCount) {
AlarmManager alarm =
(AlarmManager)AAplication.Context.GetSystemService(Context.AlarmService);
for (int i = 0; i < maxCount; i++) {
if (i < reminders.Count) {
TriggeredReminder reminder = reminders[i];
var pendingIntent = PendingIntent.GetBroadcast(AAplication.Context, i,
CreateIntent(reminder),
PendingIntentFlags.UpdateCurrent);
alarm.Cancel(pendingIntent);
AlarmManagerCompat.SetExactAndAllowWhileIdle(alarm,
(int)AlarmType.RtcWakeup,
ToNativeDate(reminder.AlertTime).Time,
pendingIntent);
}
else {
var pendingIntent = PendingIntent.GetBroadcast(AAplication.Context, i,
CreateIntent(),
PendingIntentFlags.UpdateCurrent);
alarm.Cancel(pendingIntent);
}
}
}
Intent CreateIntent() {
return new Intent(AAplication.Context, typeof(NotificationAlarmHandler)).
SetAction(NotificationAlarmHandler.NotificationHandler);
}
Intent CreateIntent(string id, int recurrenceIndex, string subject, string interval) {
return CreateIntent()
.PutExtra(NotificationAlarmHandler.ReminderId, id)
.PutExtra(NotificationAlarmHandler.RecurrenceIndex, recurrenceIndex)
.PutExtra(NotificationAlarmHandler.Subject, subject)
.PutExtra(NotificationAlarmHandler.Interval, interval);
}
Intent CreateIntent(TriggeredReminder reminder) {
AppointmentItem appointment = reminder.Appointment;
return CreateIntent(reminder.Id.ToString(),
appointment.RecurrenceIndex,
appointment.Subject,
appointment.Interval.ToString("{0:g} - {1:g}", null));
}
}
}