@@ -15,36 +15,33 @@ public class AndroidNotificationManager : INotificationManager
15
15
const string channelId = "default" ;
16
16
const string channelName = "Default" ;
17
17
const string channelDescription = "The default channel for notifications." ;
18
- const int pendingIntentId = 0 ;
19
18
20
19
public const string TitleKey = "title" ;
21
20
public const string MessageKey = "message" ;
22
21
23
22
bool channelInitialized = false ;
24
- int messageId = - 1 ;
23
+ int messageId = 0 ;
24
+ int pendingIntentId = 0 ;
25
+
25
26
NotificationManager manager ;
26
27
27
28
public event EventHandler NotificationReceived ;
28
29
30
+ public static AndroidNotificationManager Instance { get ; private set ; }
31
+
29
32
public void Initialize ( )
30
33
{
31
34
CreateNotificationChannel ( ) ;
35
+ Instance = this ;
32
36
}
33
37
34
- public int ScheduleNotification ( string title , string message )
38
+ public void Show ( string title , string message )
35
39
{
36
- if ( ! channelInitialized )
37
- {
38
- CreateNotificationChannel ( ) ;
39
- }
40
-
41
- messageId ++ ;
42
-
43
40
Intent intent = new Intent ( AndroidApp . Context , typeof ( MainActivity ) ) ;
44
41
intent . PutExtra ( TitleKey , title ) ;
45
42
intent . PutExtra ( MessageKey , message ) ;
46
43
47
- PendingIntent pendingIntent = PendingIntent . GetActivity ( AndroidApp . Context , pendingIntentId , intent , PendingIntentFlags . OneShot ) ;
44
+ PendingIntent pendingIntent = PendingIntent . GetActivity ( AndroidApp . Context , pendingIntentId ++ , intent , PendingIntentFlags . UpdateCurrent ) ;
48
45
49
46
NotificationCompat . Builder builder = new NotificationCompat . Builder ( AndroidApp . Context , channelId )
50
47
. SetContentIntent ( pendingIntent )
@@ -55,9 +52,31 @@ public int ScheduleNotification(string title, string message)
55
52
. SetDefaults ( ( int ) NotificationDefaults . Sound | ( int ) NotificationDefaults . Vibrate ) ;
56
53
57
54
Notification notification = builder . Build ( ) ;
58
- manager . Notify ( messageId , notification ) ;
55
+ manager . Notify ( messageId ++ , notification ) ;
56
+ }
59
57
60
- return messageId ;
58
+ public void SendNotification ( string title , string message , DateTime ? notifyTime = null )
59
+ {
60
+ if ( ! channelInitialized )
61
+ {
62
+ CreateNotificationChannel ( ) ;
63
+ }
64
+
65
+ if ( notifyTime != null )
66
+ {
67
+ Intent intent = new Intent ( AndroidApp . Context , typeof ( AlarmHandler ) ) ;
68
+ intent . PutExtra ( TitleKey , title ) ;
69
+ intent . PutExtra ( MessageKey , message ) ;
70
+
71
+ PendingIntent pendingIntent = PendingIntent . GetBroadcast ( AndroidApp . Context , pendingIntentId ++ , intent , PendingIntentFlags . CancelCurrent ) ;
72
+ long triggerTime = GetNotifyTime ( notifyTime . Value ) ;
73
+ AlarmManager alarmManager = GetAlarmManager ( ) ;
74
+ alarmManager . Set ( AlarmType . RtcWakeup , triggerTime , pendingIntent ) ;
75
+ }
76
+ else
77
+ {
78
+ Show ( title , message ) ;
79
+ }
61
80
}
62
81
63
82
public void ReceiveNotification ( string title , string message )
@@ -86,5 +105,19 @@ void CreateNotificationChannel()
86
105
87
106
channelInitialized = true ;
88
107
}
108
+
109
+ AlarmManager GetAlarmManager ( )
110
+ {
111
+ AlarmManager alarmManager = Android . App . Application . Context . GetSystemService ( Context . AlarmService ) as AlarmManager ;
112
+ return alarmManager ;
113
+ }
114
+
115
+ long GetNotifyTime ( DateTime notifyTime )
116
+ {
117
+ DateTime utcTime = TimeZoneInfo . ConvertTimeToUtc ( notifyTime ) ;
118
+ double epochDiff = ( new DateTime ( 1970 , 1 , 1 ) - DateTime . MinValue ) . TotalSeconds ;
119
+ long utcAlarmTime = utcTime . AddSeconds ( - epochDiff ) . Ticks / 10000 ;
120
+ return utcAlarmTime ; // milliseconds
121
+ }
89
122
}
90
123
}
0 commit comments