Skip to content

Commit

Permalink
feat(PushNotifications): Make register method return if permission wa…
Browse files Browse the repository at this point in the history
…s granted (#2324)
  • Loading branch information
chrisweight authored Feb 4, 2020
1 parent 599c5c4 commit a0bcf5c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1486,8 +1486,12 @@ export interface PushNotificationChannelList {
channels: PushNotificationChannel[];
}

export interface PushNotificationRegistrationResponse {
granted: boolean;
}

export interface PushNotificationsPlugin extends Plugin {
register(): Promise<void>;
register(): Promise<PushNotificationRegistrationResponse>;
getDeliveredNotifications(): Promise<PushNotificationDeliveredList>;
removeDeliveredNotifications(delivered: PushNotificationDeliveredList): Promise<void>;
removeAllDeliveredNotifications(): Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
16 changes: 11 additions & 5 deletions ios/Capacitor/Capacitor/Plugins/PushNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
}
}

/**
Expand All @@ -40,7 +46,7 @@ public class CAPPushNotificationsPlugin : CAPPlugin {
])
})
}

/**
* Remove specified notifications from Notification Center
*/
Expand Down Expand Up @@ -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)})
Expand Down
4 changes: 2 additions & 2 deletions site/src/assets/docs-content/apis/push-notifications/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ <h3 class="avc-code-method-header" id="method-listChannels-0">listChannels</h3>
<div class="avc-code-method">
<h3 class="avc-code-method-header" id="method-register-0">register</h3>
<div class="avc-code-method-signature">
<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>
<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>
</div>

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

<div class="avc-code-method-returns-info">
<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>
<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>
</div>

</div></div>
Expand Down

0 comments on commit a0bcf5c

Please sign in to comment.