Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PushNotifications): Make register method to return if permission was granted #2324

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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 @@ -1457,8 +1457,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