Skip to content

Commit

Permalink
feat: add requestPermission to PushNotifications and LocalNotificatio…
Browse files Browse the repository at this point in the history
…ns (#2516)
  • Loading branch information
jcesarmobile authored Mar 3, 2020
1 parent 23093f4 commit 82e38a4
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,12 +1081,17 @@ export interface LocalNotificationEnabledResult {
value: boolean;
}

export interface NotificationPermissionResponse {
granted: boolean;
}

export interface LocalNotificationsPlugin extends Plugin {
schedule(options: { notifications: LocalNotification[] }): Promise<LocalNotificationScheduleResult>;
getPending(): Promise<LocalNotificationPendingList>;
registerActionTypes(options: { types: LocalNotificationActionType[] }): Promise<void>;
cancel(pending: LocalNotificationPendingList): Promise<void>;
areEnabled(): Promise<LocalNotificationEnabledResult>;
requestPermission(): Promise<NotificationPermissionResponse>;
addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void): PluginListenerHandle;
addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void): PluginListenerHandle;
}
Expand Down Expand Up @@ -1489,12 +1494,9 @@ export interface PushNotificationChannelList {
channels: PushNotificationChannel[];
}

export interface PushNotificationRegistrationResponse {
granted: boolean;
}

export interface PushNotificationsPlugin extends Plugin {
register(): Promise<PushNotificationRegistrationResponse>;
register(): Promise<void>;
requestPermission(): Promise<NotificationPermissionResponse>;
getDeliveredNotifications(): Promise<PushNotificationDeliveredList>;
removeDeliveredNotifications(delivered: PushNotificationDeliveredList): Promise<void>;
removeAllDeliveredNotifications(): Promise<void>;
Expand Down
14 changes: 13 additions & 1 deletion core/src/web/local-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
LocalNotificationPendingList,
LocalNotificationActionType,
LocalNotification,
LocalNotificationScheduleResult
LocalNotificationScheduleResult,
NotificationPermissionResponse
} from '../core-plugin-definitions';

import { PermissionsRequestResult } from '../definitions';
Expand Down Expand Up @@ -95,6 +96,17 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif
throw new Error('Method not implemented.');
}

requestPermission(): Promise<NotificationPermissionResponse> {
return new Promise((resolve) => {
Notification.requestPermission((result) => {
let granted = true;
if (result === 'denied' || result === 'default') {
granted = false;
}
resolve({granted});
});
});
}

requestPermissions(): Promise<PermissionsRequestResult> {
return new Promise((resolve, reject) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
18 changes: 14 additions & 4 deletions ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
*/
Expand Down
11 changes: 10 additions & 1 deletion ios/Capacitor/Capacitor/Plugins/PushNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Expand Down

0 comments on commit 82e38a4

Please sign in to comment.