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: add requestPermission to PushNotifications and LocalNotifications #2516

Merged
merged 1 commit into from
Mar 3, 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 @@ -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