1
- import * as app from " tns-core-modules/application" ;
2
- import * as utils from " tns-core-modules/utils/utils" ;
1
+ import * as app from ' tns-core-modules/application' ;
2
+ import * as utils from ' tns-core-modules/utils/utils' ;
3
3
import {
4
4
LocalNotificationsApi ,
5
5
LocalNotificationsCommon ,
6
6
ReceivedNotification ,
7
7
ScheduleInterval ,
8
8
ScheduleOptions
9
- } from " ./local-notifications-common" ;
9
+ } from ' ./local-notifications-common' ;
10
10
11
11
declare const android , com , global : any ;
12
12
13
- const NotificationManagerCompatPackageName = useAndroidX ( ) ? global . androidx . core . app : android . support . v4 . app ;
13
+ const NotificationManagerCompatPackageName = useAndroidX ( )
14
+ ? global . androidx . core . app
15
+ : android . support . v4 . app ;
14
16
15
- function useAndroidX ( ) {
17
+ function useAndroidX ( ) {
16
18
return global . androidx && global . androidx . appcompat ;
17
19
}
18
20
19
21
( ( ) => {
20
22
const registerLifecycleEvents = ( ) => {
21
- com . telerik . localnotifications . LifecycleCallbacks . registerCallbacks ( app . android . nativeApp ) ;
23
+ com . telerik . localnotifications . LifecycleCallbacks . registerCallbacks (
24
+ app . android . nativeApp
25
+ ) ;
22
26
} ;
23
27
24
28
// Hook on the application events
@@ -29,47 +33,75 @@ function useAndroidX () {
29
33
}
30
34
} ) ( ) ;
31
35
32
- export class LocalNotificationsImpl extends LocalNotificationsCommon implements LocalNotificationsApi {
33
-
34
- private static IS_GTE_LOLLIPOP : boolean = android . os . Build . VERSION . SDK_INT >= 21 ;
36
+ export class LocalNotificationsImpl extends LocalNotificationsCommon
37
+ implements LocalNotificationsApi {
38
+ private static IS_GTE_LOLLIPOP : boolean =
39
+ android . os . Build . VERSION . SDK_INT >= 21 ;
35
40
36
41
private static getInterval ( interval : ScheduleInterval ) : number {
37
- if ( interval === " second" ) {
42
+ if ( interval === ' second' ) {
38
43
return 1000 ; // it's in ms
39
- } else if ( interval === " minute" ) {
44
+ } else if ( interval === ' minute' ) {
40
45
return android . app . AlarmManager . INTERVAL_FIFTEEN_MINUTES / 15 ;
41
- } else if ( interval === " hour" ) {
46
+ } else if ( interval === ' hour' ) {
42
47
return android . app . AlarmManager . INTERVAL_HOUR ;
43
- } else if ( interval === " day" ) {
48
+ } else if ( interval === ' day' ) {
44
49
return android . app . AlarmManager . INTERVAL_DAY ;
45
- } else if ( interval === " week" ) {
50
+ } else if ( interval === ' week' ) {
46
51
return android . app . AlarmManager . INTERVAL_DAY * 7 ;
47
- } else if ( interval === " month" ) {
52
+ } else if ( interval === ' month' ) {
48
53
return android . app . AlarmManager . INTERVAL_DAY * 31 ; // well that's almost accurate
49
- } else if ( interval === " year" ) {
54
+ } else if ( interval === ' year' ) {
50
55
return android . app . AlarmManager . INTERVAL_DAY * 365 ; // same here
51
56
} else {
52
57
return undefined ;
53
58
}
54
59
}
55
60
56
- private static getIcon ( context : any /* android.content.Context */ , resources : any , iconLocation ?: string ) : string {
61
+ private static getIcon (
62
+ context : any /* android.content.Context */ ,
63
+ resources : any ,
64
+ iconLocation ?: string
65
+ ) : string {
57
66
const packageName : string = context . getApplicationInfo ( ) . packageName ;
58
- return iconLocation
59
- && iconLocation . indexOf ( utils . RESOURCE_PREFIX ) === 0
60
- && resources . getIdentifier ( iconLocation . substr ( utils . RESOURCE_PREFIX . length ) , "drawable" , packageName )
61
- || ( LocalNotificationsImpl . IS_GTE_LOLLIPOP && resources . getIdentifier ( "ic_stat_notify_silhouette" , "drawable" , packageName ) )
62
- || resources . getIdentifier ( "ic_stat_notify" , "drawable" , packageName )
63
- || context . getApplicationInfo ( ) . icon ;
67
+ return (
68
+ ( iconLocation &&
69
+ iconLocation . indexOf ( utils . RESOURCE_PREFIX ) === 0 &&
70
+ resources . getIdentifier (
71
+ iconLocation . substr ( utils . RESOURCE_PREFIX . length ) ,
72
+ 'drawable' ,
73
+ packageName
74
+ ) ) ||
75
+ ( LocalNotificationsImpl . IS_GTE_LOLLIPOP &&
76
+ resources . getIdentifier (
77
+ 'ic_stat_notify_silhouette' ,
78
+ 'drawable' ,
79
+ packageName
80
+ ) ) ||
81
+ resources . getIdentifier ( 'ic_stat_notify' , 'drawable' , packageName ) ||
82
+ context . getApplicationInfo ( ) . icon
83
+ ) ;
64
84
}
65
85
66
86
private static cancelById ( id : number ) : void {
67
87
const context = utils . ad . getApplicationContext ( ) ;
68
- const notificationIntent = new android . content . Intent ( context , com . telerik . localnotifications . NotificationAlarmReceiver . class ) . setAction ( "" + id ) ;
69
- const pendingIntent = android . app . PendingIntent . getBroadcast ( context , 0 , notificationIntent , 0 ) ;
70
- const alarmManager = context . getSystemService ( android . content . Context . ALARM_SERVICE ) ;
88
+ const notificationIntent = new android . content . Intent (
89
+ context ,
90
+ com . telerik . localnotifications . NotificationAlarmReceiver . class
91
+ ) . setAction ( '' + id ) ;
92
+ const pendingIntent = android . app . PendingIntent . getBroadcast (
93
+ context ,
94
+ 0 ,
95
+ notificationIntent ,
96
+ 0
97
+ ) ;
98
+ const alarmManager = context . getSystemService (
99
+ android . content . Context . ALARM_SERVICE
100
+ ) ;
71
101
alarmManager . cancel ( pendingIntent ) ;
72
- const notificationManager = context . getSystemService ( android . content . Context . NOTIFICATION_SERVICE ) ;
102
+ const notificationManager = context . getSystemService (
103
+ android . content . Context . NOTIFICATION_SERVICE
104
+ ) ;
73
105
notificationManager . cancel ( id ) ;
74
106
75
107
com . telerik . localnotifications . Store . remove ( context , id ) ;
@@ -80,10 +112,12 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
80
112
try {
81
113
// nothing to do on this platform
82
114
const context = utils . ad . getApplicationContext ( ) ;
83
- const hasPermission = NotificationManagerCompatPackageName . NotificationManagerCompat . from ( context ) . areNotificationsEnabled ( ) ;
115
+ const hasPermission = NotificationManagerCompatPackageName . NotificationManagerCompat . from (
116
+ context
117
+ ) . areNotificationsEnabled ( ) ;
84
118
resolve ( hasPermission ) ;
85
119
} catch ( ex ) {
86
- console . log ( " Error in LocalNotifications.hasPermission: " + ex ) ;
120
+ console . log ( ' Error in LocalNotifications.hasPermission: ' + ex ) ;
87
121
reject ( ex ) ;
88
122
}
89
123
} ) ;
@@ -95,45 +129,53 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
95
129
// nothing to do on this platform
96
130
resolve ( true ) ;
97
131
} catch ( ex ) {
98
- console . log ( " Error in LocalNotifications.requestPermission: " + ex ) ;
132
+ console . log ( ' Error in LocalNotifications.requestPermission: ' + ex ) ;
99
133
reject ( ex ) ;
100
134
}
101
135
} ) ;
102
136
}
103
137
104
- addOnMessageReceivedCallback ( onReceived : ( data : ReceivedNotification ) => void ) : Promise < any > {
138
+ addOnMessageReceivedCallback (
139
+ onReceived : ( data : ReceivedNotification ) => void
140
+ ) : Promise < any > {
105
141
return new Promise ( ( resolve , reject ) => {
106
142
try {
107
143
// note that this is ONLY triggered when the user clicked the notification in the statusbar
108
144
com . telerik . localnotifications . LocalNotificationsPlugin . setOnMessageReceivedCallback (
109
- new com . telerik . localnotifications . LocalNotificationsPluginListener ( {
110
- success : notification => {
111
- onReceived ( JSON . parse ( notification ) ) ;
112
- }
113
- } )
145
+ new com . telerik . localnotifications . LocalNotificationsPluginListener ( {
146
+ success : notification => {
147
+ onReceived ( JSON . parse ( notification ) ) ;
148
+ }
149
+ } )
114
150
) ;
115
151
resolve ( ) ;
116
152
} catch ( ex ) {
117
- console . log ( "Error in LocalNotifications.addOnMessageReceivedCallback: " + ex ) ;
153
+ console . log (
154
+ 'Error in LocalNotifications.addOnMessageReceivedCallback: ' + ex
155
+ ) ;
118
156
reject ( ex ) ;
119
157
}
120
158
} ) ;
121
159
}
122
160
123
- addOnMessageClearedCallback ( onReceived : ( data : ReceivedNotification ) => void ) : Promise < any > {
161
+ addOnMessageClearedCallback (
162
+ onReceived : ( data : ReceivedNotification ) => void
163
+ ) : Promise < any > {
124
164
return new Promise ( ( resolve , reject ) => {
125
165
try {
126
166
// note that this is ONLY triggered when the user clicked the notification in the statusbar
127
167
com . telerik . localnotifications . LocalNotificationsPlugin . setOnMessageClearedCallback (
128
- new com . telerik . localnotifications . LocalNotificationsPluginListener ( {
129
- success : notification => {
130
- onReceived ( JSON . parse ( notification ) ) ;
131
- }
132
- } )
168
+ new com . telerik . localnotifications . LocalNotificationsPluginListener ( {
169
+ success : notification => {
170
+ onReceived ( JSON . parse ( notification ) ) ;
171
+ }
172
+ } )
133
173
) ;
134
174
resolve ( ) ;
135
175
} catch ( ex ) {
136
- console . log ( "Error in LocalNotifications.addOnMessageClearedCallback: " + ex ) ;
176
+ console . log (
177
+ 'Error in LocalNotifications.addOnMessageClearedCallback: ' + ex
178
+ ) ;
137
179
reject ( ex ) ;
138
180
}
139
181
} ) ;
@@ -145,7 +187,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
145
187
LocalNotificationsImpl . cancelById ( id ) ;
146
188
resolve ( true ) ;
147
189
} catch ( ex ) {
148
- console . log ( " Error in LocalNotifications.cancel: " + ex ) ;
190
+ console . log ( ' Error in LocalNotifications.cancel: ' + ex ) ;
149
191
reject ( ex ) ;
150
192
}
151
193
} ) ;
@@ -163,16 +205,22 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
163
205
// console.log(">> < 26, StatusBarNotification[0]: " + new android.service.notification.StatusBarNotification[0]);
164
206
// }
165
207
166
- const keys : Array < string > = com . telerik . localnotifications . Store . getKeys ( utils . ad . getApplicationContext ( ) ) ;
208
+ const keys : Array <
209
+ string
210
+ > = com . telerik . localnotifications . Store . getKeys (
211
+ utils . ad . getApplicationContext ( )
212
+ ) ;
167
213
168
214
for ( let i = 0 ; i < keys . length ; i ++ ) {
169
215
LocalNotificationsImpl . cancelById ( parseInt ( keys [ i ] ) ) ;
170
216
}
171
217
172
- NotificationManagerCompatPackageName . NotificationManagerCompat . from ( context ) . cancelAll ( ) ;
218
+ NotificationManagerCompatPackageName . NotificationManagerCompat . from (
219
+ context
220
+ ) . cancelAll ( ) ;
173
221
resolve ( ) ;
174
222
} catch ( ex ) {
175
- console . log ( " Error in LocalNotifications.cancelAll: " + ex ) ;
223
+ console . log ( ' Error in LocalNotifications.cancelAll: ' + ex ) ;
176
224
reject ( ex ) ;
177
225
}
178
226
} ) ;
@@ -181,7 +229,11 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
181
229
getScheduledIds ( ) : Promise < number [ ] > {
182
230
return new Promise ( ( resolve , reject ) => {
183
231
try {
184
- const keys : Array < string > = com . telerik . localnotifications . Store . getKeys ( utils . ad . getApplicationContext ( ) ) ;
232
+ const keys : Array <
233
+ string
234
+ > = com . telerik . localnotifications . Store . getKeys (
235
+ utils . ad . getApplicationContext ( )
236
+ ) ;
185
237
186
238
const ids : number [ ] = [ ] ;
187
239
for ( let i = 0 ; i < keys . length ; i ++ ) {
@@ -190,7 +242,7 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
190
242
191
243
resolve ( ids ) ;
192
244
} catch ( ex ) {
193
- console . log ( " Error in LocalNotifications.getScheduledIds: " + ex ) ;
245
+ console . log ( ' Error in LocalNotifications.getScheduledIds: ' + ex ) ;
194
246
reject ( ex ) ;
195
247
}
196
248
} ) ;
@@ -207,18 +259,25 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
207
259
// the persisted options are exactly like the original ones.
208
260
209
261
for ( let n in scheduleOptions ) {
210
- const options = LocalNotificationsImpl . merge ( scheduleOptions [ n ] , LocalNotificationsImpl . defaults ) ;
262
+ const options = LocalNotificationsImpl . merge (
263
+ scheduleOptions [ n ] ,
264
+ LocalNotificationsImpl . defaults
265
+ ) ;
211
266
212
267
options . icon = LocalNotificationsImpl . getIcon (
213
- context ,
214
- resources ,
215
- LocalNotificationsImpl . IS_GTE_LOLLIPOP && options . silhouetteIcon || options . icon
268
+ context ,
269
+ resources ,
270
+ ( LocalNotificationsImpl . IS_GTE_LOLLIPOP &&
271
+ options . silhouetteIcon ) ||
272
+ options . icon
216
273
) ;
217
274
218
275
options . atTime = options . at ? options . at . getTime ( ) : 0 ;
219
276
220
277
// Used when restoring the notification after a reboot:
221
- options . repeatInterval = LocalNotificationsImpl . getInterval ( options . interval ) ;
278
+ options . repeatInterval = LocalNotificationsImpl . getInterval (
279
+ options . interval
280
+ ) ;
222
281
223
282
if ( options . color ) {
224
283
options . color = options . color . android ;
@@ -231,15 +290,16 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
231
290
LocalNotificationsImpl . ensureID ( options ) ;
232
291
233
292
com . telerik . localnotifications . LocalNotificationsPlugin . scheduleNotification (
234
- new org . json . JSONObject ( JSON . stringify ( options ) ) ,
235
- context ) ;
293
+ new org . json . JSONObject ( JSON . stringify ( options ) ) ,
294
+ context
295
+ ) ;
236
296
237
297
scheduledIds . push ( options . id ) ;
238
298
}
239
299
240
300
resolve ( scheduledIds ) ;
241
301
} catch ( ex ) {
242
- console . log ( " Error in LocalNotifications.schedule: " + ex ) ;
302
+ console . log ( ' Error in LocalNotifications.schedule: ' + ex ) ;
243
303
reject ( ex ) ;
244
304
}
245
305
} ) ;
0 commit comments