From 82e38a4f756ba69d17c607d13cf67c30f696f2cc Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Tue, 3 Mar 2020 20:11:25 +0100 Subject: [PATCH] feat: add requestPermission to PushNotifications and LocalNotifications (#2516) --- .../plugin/LocalNotifications.java | 7 +++++++ .../getcapacitor/plugin/PushNotifications.java | 5 +++++ core/src/core-plugin-definitions.ts | 12 +++++++----- core/src/web/local-notifications.ts | 14 +++++++++++++- .../CAPUNUserNotificationCenterDelegate.swift | 7 ------- .../Capacitor/Plugins/DefaultPlugins.m | 2 ++ .../Capacitor/Plugins/LocalNotifications.swift | 18 ++++++++++++++---- .../Capacitor/Plugins/PushNotifications.swift | 11 ++++++++++- 8 files changed, 58 insertions(+), 18 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java index b129990bf6..287c32011a 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java @@ -84,6 +84,13 @@ public void schedule(PluginCall call) { call.success(result); } + @PluginMethod() + public void requestPermission(PluginCall call) { + JSObject result = new JSObject(); + result.put("granted", true); + call.success(result); + } + @PluginMethod() public void cancel(PluginCall call) { manager.cancel(call); diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java index 6e74621150..18121dfeea 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java @@ -104,6 +104,11 @@ public void onFailure(Exception e) { sendError(e.getLocalizedMessage()); } }); + call.success(); + } + + @PluginMethod() + public void requestPermission(PluginCall call) { JSObject result = new JSObject(); result.put("granted", true); call.success(result); diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 24c090e0cb..eac36e8692 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1081,12 +1081,17 @@ export interface LocalNotificationEnabledResult { value: boolean; } +export interface NotificationPermissionResponse { + granted: boolean; +} + export interface LocalNotificationsPlugin extends Plugin { schedule(options: { notifications: LocalNotification[] }): Promise; getPending(): Promise; registerActionTypes(options: { types: LocalNotificationActionType[] }): Promise; cancel(pending: LocalNotificationPendingList): Promise; areEnabled(): Promise; + requestPermission(): Promise; addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void): PluginListenerHandle; addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void): PluginListenerHandle; } @@ -1489,12 +1494,9 @@ export interface PushNotificationChannelList { channels: PushNotificationChannel[]; } -export interface PushNotificationRegistrationResponse { - granted: boolean; -} - export interface PushNotificationsPlugin extends Plugin { - register(): Promise; + register(): Promise; + requestPermission(): Promise; getDeliveredNotifications(): Promise; removeDeliveredNotifications(delivered: PushNotificationDeliveredList): Promise; removeAllDeliveredNotifications(): Promise; diff --git a/core/src/web/local-notifications.ts b/core/src/web/local-notifications.ts index 8ae1636a77..d22383b155 100644 --- a/core/src/web/local-notifications.ts +++ b/core/src/web/local-notifications.ts @@ -6,7 +6,8 @@ import { LocalNotificationPendingList, LocalNotificationActionType, LocalNotification, - LocalNotificationScheduleResult + LocalNotificationScheduleResult, + NotificationPermissionResponse } from '../core-plugin-definitions'; import { PermissionsRequestResult } from '../definitions'; @@ -95,6 +96,17 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif throw new Error('Method not implemented.'); } + requestPermission(): Promise { + return new Promise((resolve) => { + Notification.requestPermission((result) => { + let granted = true; + if (result === 'denied' || result === 'default') { + granted = false; + } + resolve({granted}); + }); + }); + } requestPermissions(): Promise { return new Promise((resolve, reject) => { diff --git a/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift b/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift index b9da579fa6..d9838dea5f 100644 --- a/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift +++ b/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift @@ -22,15 +22,8 @@ public class CAPUNUserNotificationCenterDelegate : NSObject, UNUserNotificationC * Request permissions to send notifications */ public func requestPermissions(with completion: ((Bool, Error?) -> Void)? = nil) { - // Override point for customization after application launch. let center = UNUserNotificationCenter.current() center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in - if granted { - DispatchQueue.main.async { - UIApplication.shared.registerForRemoteNotifications() - } - } - completion?(granted, error) } } diff --git a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m index 8d8b4e895a..4a0342394a 100644 --- a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m +++ b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m @@ -84,6 +84,7 @@ CAP_PLUGIN(CAPLocalNotificationsPlugin, "LocalNotifications", CAP_PLUGIN_METHOD(schedule, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(requestPermission, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(cancel, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getPending, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(registerActionTypes, CAPPluginReturnPromise); @@ -107,6 +108,7 @@ CAP_PLUGIN(CAPPushNotificationsPlugin, "PushNotifications", CAP_PLUGIN_METHOD(register, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(requestPermission, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getDeliveredNotifications, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(removeDeliveredNotifications, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(removeAllDeliveredNotifications, CAPPluginReturnPromise); diff --git a/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift b/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift index a8f578d100..0b52f39175 100644 --- a/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift +++ b/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift @@ -37,9 +37,6 @@ public class CAPLocalNotificationsPlugin : CAPPlugin { call.error("Must provide notifications array as notifications option") return } - - self.bridge.notificationsDelegate.requestPermissions() - var ids = [String]() for notification in notifications { @@ -95,7 +92,20 @@ public class CAPLocalNotificationsPlugin : CAPPlugin { "notifications": ret ]) } - + + /** + * Request notification permission + */ + @objc func requestPermission(_ call: CAPPluginCall) { + self.bridge.notificationsDelegate.requestPermissions() { granted, error in + guard error == nil else { + call.error(error!.localizedDescription) + return + } + call.success(["granted": granted]) + } + } + /** * Cancel notifications by id */ diff --git a/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift b/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift index 6f79a9aca2..0e6b9086be 100644 --- a/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift +++ b/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift @@ -23,12 +23,21 @@ public class CAPPushNotificationsPlugin : CAPPlugin { * Register for push notifications */ @objc func register(_ call: CAPPluginCall) { + DispatchQueue.main.async { + UIApplication.shared.registerForRemoteNotifications() + } + call.success() + } + + /** + * Request notification permission + */ + @objc func requestPermission(_ call: CAPPluginCall) { self.bridge.notificationsDelegate.requestPermissions() { granted, error in guard error == nil else { call.error(error!.localizedDescription) return } - call.success(["granted": granted]) } }