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 bc3efd6cd5..6e74621150 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java @@ -104,7 +104,9 @@ public void onFailure(Exception e) { sendError(e.getLocalizedMessage()); } }); - call.success(); + JSObject result = new JSObject(); + result.put("granted", true); + call.success(result); } @PluginMethod() diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 8400ecb144..6107ac14df 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1486,8 +1486,12 @@ export interface PushNotificationChannelList { channels: PushNotificationChannel[]; } +export interface PushNotificationRegistrationResponse { + granted: boolean; +} + export interface PushNotificationsPlugin extends Plugin { - register(): Promise; + register(): Promise; getDeliveredNotifications(): Promise; removeDeliveredNotifications(delivered: PushNotificationDeliveredList): Promise; removeAllDeliveredNotifications(): Promise; diff --git a/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift b/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift index ed07c0a0eb..b9da579fa6 100644 --- a/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift +++ b/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift @@ -21,15 +21,17 @@ public class CAPUNUserNotificationCenterDelegate : NSObject, UNUserNotificationC /** * Request permissions to send notifications */ - public func requestPermissions() { + 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 - // Enable or disable features based on authorization. - } + if granted { + DispatchQueue.main.async { + UIApplication.shared.registerForRemoteNotifications() + } + } - DispatchQueue.main.async { - UIApplication.shared.registerForRemoteNotifications() + completion?(granted, error) } } diff --git a/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift b/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift index f79f46bf8f..6f79a9aca2 100644 --- a/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift +++ b/ios/Capacitor/Capacitor/Plugins/PushNotifications.swift @@ -13,7 +13,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin { // Local list of notification id -> JSObject for storing options // between notification requets var notificationRequestLookup = [String:JSObject]() - + public override func load() { NotificationCenter.default.addObserver(self, selector: #selector(self.didRegisterForRemoteNotificationsWithDeviceToken(notification:)), name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.didFailToRegisterForRemoteNotificationsWithError(notification:)), name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: nil) @@ -23,8 +23,14 @@ public class CAPPushNotificationsPlugin : CAPPlugin { * Register for push notifications */ @objc func register(_ call: CAPPluginCall) { - self.bridge.notificationsDelegate.requestPermissions() - call.success() + self.bridge.notificationsDelegate.requestPermissions() { granted, error in + guard error == nil else { + call.error(error!.localizedDescription) + return + } + + call.success(["granted": granted]) + } } /** @@ -40,7 +46,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin { ]) }) } - + /** * Remove specified notifications from Notification Center */ @@ -78,7 +84,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin { @objc func listChannels(_ call: CAPPluginCall) { call.unimplemented() } - + @objc public func didRegisterForRemoteNotificationsWithDeviceToken(notification: NSNotification){ if let deviceToken = notification.object as? Data { let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) diff --git a/site/src/assets/docs-content/apis/push-notifications/api.html b/site/src/assets/docs-content/apis/push-notifications/api.html index fcca2d6349..db845ea65f 100644 --- a/site/src/assets/docs-content/apis/push-notifications/api.html +++ b/site/src/assets/docs-content/apis/push-notifications/api.html @@ -66,13 +66,13 @@

listChannels

register

- register(): Promise<void> + register(): Promise<PushNotificationRegistrationResponse>
- Returns: Promise<void> + Returns: Promise<PushNotificationRegistrationResponse>