|
1 | 1 | import { Application, Device, Utils } from '@nativescript/core';
|
| 2 | +import { check, request, Result } from '@nativescript-community/perms'; |
2 | 3 | import { LocalNotificationsApi, LocalNotificationsCommon, ReceivedNotification, ScheduleInterval, ScheduleOptions } from './common';
|
3 | 4 |
|
4 | 5 | declare const com, global: any;
|
@@ -68,27 +69,23 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
|
68 | 69 | com.telerik.localnotifications.Store.remove(context, id);
|
69 | 70 | }
|
70 | 71 |
|
71 |
| - hasPermission(): Promise<boolean> { |
72 |
| - return new Promise((resolve, reject) => { |
73 |
| - try { |
74 |
| - resolve(LocalNotificationsImpl.hasPermission()); |
75 |
| - } catch (ex) { |
76 |
| - console.log('Error in LocalNotifications.hasPermission: ' + ex); |
77 |
| - reject(ex); |
78 |
| - } |
79 |
| - }); |
| 72 | + async hasPermission(): Promise<boolean> { |
| 73 | + try { |
| 74 | + return await LocalNotificationsImpl.canSend(); |
| 75 | + } catch (ex) { |
| 76 | + console.log('Error in LocalNotifications.hasPermission: ' + ex); |
| 77 | + throw ex; |
| 78 | + } |
80 | 79 | }
|
81 | 80 |
|
82 |
| - requestPermission(): Promise<boolean> { |
83 |
| - return new Promise((resolve, reject) => { |
84 |
| - try { |
85 |
| - // AFAIK can't do it on this platform.. when 'false' is returned, the app could prompt the user to manually enable them in the Device Settings |
86 |
| - resolve(LocalNotificationsImpl.hasPermission()); |
87 |
| - } catch (ex) { |
88 |
| - console.log('Error in LocalNotifications.requestPermission: ' + ex); |
89 |
| - reject(ex); |
90 |
| - } |
91 |
| - }); |
| 81 | + async requestPermission(): Promise<boolean> { |
| 82 | + try { |
| 83 | + await LocalNotificationsImpl.ensurePreconditions(); |
| 84 | + return true; |
| 85 | + } catch (ex) { |
| 86 | + console.log('Error in LocalNotifications.requestPermission: ' + ex); |
| 87 | + return false; |
| 88 | + } |
92 | 89 | }
|
93 | 90 |
|
94 | 91 | addOnMessageReceivedCallback(onReceived: (data: ReceivedNotification) => void): Promise<void> {
|
@@ -186,58 +183,89 @@ export class LocalNotificationsImpl extends LocalNotificationsCommon implements
|
186 | 183 | });
|
187 | 184 | }
|
188 | 185 |
|
189 |
| - schedule(scheduleOptions: ScheduleOptions[]): Promise<Array<number>> { |
190 |
| - return new Promise((resolve, reject) => { |
191 |
| - try { |
192 |
| - if (!LocalNotificationsImpl.hasPermission()) { |
193 |
| - reject('Permission not granted'); |
194 |
| - return; |
195 |
| - } |
196 |
| - |
197 |
| - const context = Utils.android.getApplicationContext(); |
198 |
| - const resources = context.getResources(); |
199 |
| - const scheduledIds: Array<number> = []; |
| 186 | + async schedule(scheduleOptions: ScheduleOptions[]): Promise<Array<number>> { |
| 187 | + try { |
| 188 | + await LocalNotificationsImpl.ensurePreconditions(); |
200 | 189 |
|
201 |
| - // TODO: All these changes in the options (other than setting the ID) should rather be done in Java so that |
202 |
| - // the persisted options are exactly like the original ones. |
| 190 | + const context = Utils.android.getApplicationContext(); |
| 191 | + const resources = context.getResources(); |
| 192 | + const scheduledIds: Array<number> = []; |
203 | 193 |
|
204 |
| - for (let n in scheduleOptions) { |
205 |
| - const options = LocalNotificationsImpl.merge(scheduleOptions[n], LocalNotificationsImpl.defaults); |
| 194 | + // TODO: All these changes in the options (other than setting the ID) should rather be done in Java so that |
| 195 | + // the persisted options are exactly like the original ones. |
206 | 196 |
|
207 |
| - options.icon = LocalNotificationsImpl.getIcon(context, resources, (LocalNotificationsImpl.IS_GTE_LOLLIPOP && options.silhouetteIcon) || options.icon); |
| 197 | + for (let n in scheduleOptions) { |
| 198 | + const options = LocalNotificationsImpl.merge(scheduleOptions[n], LocalNotificationsImpl.defaults); |
208 | 199 |
|
209 |
| - options.atTime = options.at ? options.at.getTime() : 0; |
| 200 | + options.icon = LocalNotificationsImpl.getIcon(context, resources, (LocalNotificationsImpl.IS_GTE_LOLLIPOP && options.silhouetteIcon) || options.icon); |
210 | 201 |
|
211 |
| - // Used when restoring the notification after a reboot: |
212 |
| - options.repeatInterval = LocalNotificationsImpl.getInterval(options.interval); |
| 202 | + options.atTime = options.at ? options.at.getTime() : 0; |
213 | 203 |
|
214 |
| - if (options.color) { |
215 |
| - options.color = options.color.android; |
216 |
| - } |
| 204 | + // Used when restoring the notification after a reboot: |
| 205 | + options.repeatInterval = LocalNotificationsImpl.getInterval(options.interval); |
217 | 206 |
|
218 |
| - if (options.notificationLed && options.notificationLed !== true) { |
219 |
| - options.notificationLed = options.notificationLed.android; |
220 |
| - } |
| 207 | + if (options.color) { |
| 208 | + options.color = options.color.android; |
| 209 | + } |
221 | 210 |
|
222 |
| - LocalNotificationsImpl.ensureID(options); |
| 211 | + if (options.notificationLed && options.notificationLed !== true) { |
| 212 | + options.notificationLed = options.notificationLed.android; |
| 213 | + } |
223 | 214 |
|
224 |
| - com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(new org.json.JSONObject(JSON.stringify(options)), context); |
| 215 | + LocalNotificationsImpl.ensureID(options); |
225 | 216 |
|
226 |
| - scheduledIds.push(options.id); |
227 |
| - } |
| 217 | + com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(new org.json.JSONObject(JSON.stringify(options)), context); |
228 | 218 |
|
229 |
| - resolve(scheduledIds); |
230 |
| - } catch (ex) { |
231 |
| - console.log('Error in LocalNotifications.schedule: ' + ex); |
232 |
| - reject(ex); |
| 219 | + scheduledIds.push(options.id); |
233 | 220 | }
|
234 |
| - }); |
| 221 | + |
| 222 | + return scheduledIds; |
| 223 | + } catch (ex) { |
| 224 | + console.log('Error in LocalNotifications.schedule: ' + ex); |
| 225 | + throw ex; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private static async ensurePreconditions(): Promise<void> { |
| 230 | + const hasPermission = await LocalNotificationsImpl.hasPermission(); |
| 231 | + if (!hasPermission) { |
| 232 | + const granted = await LocalNotificationsImpl.requestPermission(); |
| 233 | + if (!granted) throw new Error('Permission not granted'); |
| 234 | + } |
| 235 | + // AFAIK can't do it on this platform. when 'false' is returned, the app could prompt the user to manually enable them in the Device Settings |
| 236 | + const enabled = LocalNotificationsImpl.areEnabled(); |
| 237 | + if (!enabled) throw new Error('Notifications were manually disabled'); |
235 | 238 | }
|
236 | 239 |
|
237 |
| - private static hasPermission(): boolean { |
| 240 | + private static async canSend(): Promise<boolean> { |
| 241 | + const hasPermission = await LocalNotificationsImpl.hasPermission(); |
| 242 | + const areEnabled = LocalNotificationsImpl.areEnabled(); |
| 243 | + return hasPermission && areEnabled; |
| 244 | + } |
| 245 | + |
| 246 | + private static areEnabled(): boolean { |
238 | 247 | const context = Utils.android.getApplicationContext();
|
239 | 248 | return !context || NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).areNotificationsEnabled();
|
240 | 249 | }
|
| 250 | + |
| 251 | + private static async hasPermission(): Promise<boolean> { |
| 252 | + const result = await check('notification'); |
| 253 | + return LocalNotificationsImpl.isAuthorized(result); |
| 254 | + } |
| 255 | + |
| 256 | + private static async requestPermission(): Promise<boolean> { |
| 257 | + try { |
| 258 | + const result = await request('notification'); |
| 259 | + return LocalNotificationsImpl.isAuthorized(result); |
| 260 | + } catch (ex) { |
| 261 | + return false; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + private static isAuthorized(result: Result): boolean { |
| 266 | + const [status, _] = result; |
| 267 | + return status === 'authorized'; |
| 268 | + } |
241 | 269 | }
|
242 | 270 |
|
243 | 271 | export const LocalNotifications = new LocalNotificationsImpl();
|
0 commit comments