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