Skip to content

Commit a0bcf5c

Browse files
authored
feat(PushNotifications): Make register method return if permission was granted (#2324)
1 parent 599c5c4 commit a0bcf5c

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ public void onFailure(Exception e) {
104104
sendError(e.getLocalizedMessage());
105105
}
106106
});
107-
call.success();
107+
JSObject result = new JSObject();
108+
result.put("granted", true);
109+
call.success(result);
108110
}
109111

110112
@PluginMethod()

core/src/core-plugin-definitions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,8 +1486,12 @@ export interface PushNotificationChannelList {
14861486
channels: PushNotificationChannel[];
14871487
}
14881488

1489+
export interface PushNotificationRegistrationResponse {
1490+
granted: boolean;
1491+
}
1492+
14891493
export interface PushNotificationsPlugin extends Plugin {
1490-
register(): Promise<void>;
1494+
register(): Promise<PushNotificationRegistrationResponse>;
14911495
getDeliveredNotifications(): Promise<PushNotificationDeliveredList>;
14921496
removeDeliveredNotifications(delivered: PushNotificationDeliveredList): Promise<void>;
14931497
removeAllDeliveredNotifications(): Promise<void>;

ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ public class CAPUNUserNotificationCenterDelegate : NSObject, UNUserNotificationC
2121
/**
2222
* Request permissions to send notifications
2323
*/
24-
public func requestPermissions() {
24+
public func requestPermissions(with completion: ((Bool, Error?) -> Void)? = nil) {
2525
// Override point for customization after application launch.
2626
let center = UNUserNotificationCenter.current()
2727
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
28-
// Enable or disable features based on authorization.
29-
}
28+
if granted {
29+
DispatchQueue.main.async {
30+
UIApplication.shared.registerForRemoteNotifications()
31+
}
32+
}
3033

31-
DispatchQueue.main.async {
32-
UIApplication.shared.registerForRemoteNotifications()
34+
completion?(granted, error)
3335
}
3436
}
3537

ios/Capacitor/Capacitor/Plugins/PushNotifications.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin {
1313
// Local list of notification id -> JSObject for storing options
1414
// between notification requets
1515
var notificationRequestLookup = [String:JSObject]()
16-
16+
1717
public override func load() {
1818
NotificationCenter.default.addObserver(self, selector: #selector(self.didRegisterForRemoteNotificationsWithDeviceToken(notification:)), name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: nil)
1919
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 {
2323
* Register for push notifications
2424
*/
2525
@objc func register(_ call: CAPPluginCall) {
26-
self.bridge.notificationsDelegate.requestPermissions()
27-
call.success()
26+
self.bridge.notificationsDelegate.requestPermissions() { granted, error in
27+
guard error == nil else {
28+
call.error(error!.localizedDescription)
29+
return
30+
}
31+
32+
call.success(["granted": granted])
33+
}
2834
}
2935

3036
/**
@@ -40,7 +46,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin {
4046
])
4147
})
4248
}
43-
49+
4450
/**
4551
* Remove specified notifications from Notification Center
4652
*/
@@ -78,7 +84,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin {
7884
@objc func listChannels(_ call: CAPPluginCall) {
7985
call.unimplemented()
8086
}
81-
87+
8288
@objc public func didRegisterForRemoteNotificationsWithDeviceToken(notification: NSNotification){
8389
if let deviceToken = notification.object as? Data {
8490
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})

site/src/assets/docs-content/apis/push-notifications/api.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ <h3 class="avc-code-method-header" id="method-listChannels-0">listChannels</h3>
6666
<div class="avc-code-method">
6767
<h3 class="avc-code-method-header" id="method-register-0">register</h3>
6868
<div class="avc-code-method-signature">
69-
<span class="avc-code-method-name">register</span><span class="avc-code-paren">(</span><span class="avc-code-paren">)</span><span class="avc-code-return-type-colon">:</span> Promise<span class="avc-code-typearg-bracket">&lt;</span>void<span class="avc-code-typearg-bracket">&gt;</span>
69+
<span class="avc-code-method-name">register</span><span class="avc-code-paren">(</span><span class="avc-code-paren">)</span><span class="avc-code-return-type-colon">:</span> Promise<span class="avc-code-typearg-bracket">&lt;</span><avc-code-type type-id="861">PushNotificationRegistrationResponse</avc-code-type><span class="avc-code-typearg-bracket">&gt;</span>
7070
</div>
7171

7272
<div class="avc-code-method-params">
7373

7474
<div class="avc-code-method-returns-info">
75-
<span class="avc-code-method-returns-label">Returns:</span> Promise<span class="avc-code-typearg-bracket">&lt;</span>void<span class="avc-code-typearg-bracket">&gt;</span>
75+
<span class="avc-code-method-returns-label">Returns:</span> Promise<span class="avc-code-typearg-bracket">&lt;</span><avc-code-type type-id="861">PushNotificationRegistrationResponse</avc-code-type><span class="avc-code-typearg-bracket">&gt;</span>
7676
</div>
7777

7878
</div></div>

0 commit comments

Comments
 (0)