Commit e38a8384 by Sebastián Katzer

RegisterPermission accept a callback

parent 0fd4ac32
...@@ -126,14 +126,17 @@ window.plugin.notification.local.hasPermission(function (granted) { ...@@ -126,14 +126,17 @@ window.plugin.notification.local.hasPermission(function (granted) {
``` ```
### Register permission for local notifications ### Register permission for local notifications
Required permissions can be registered through the `notification.local.registerPermission` interface. Required permissions can be registered through the `notification.local.registerPermission` interface.<br/>
The method takes a callback function as its argument which will be called with a boolean value. Optional the scope of the callback function ca be defined through a second argument.
#### Further informations #### Further informations
- The method is supported on each platform, however its only relevant for iOS8 and above. - The method is supported on each platform, however its only relevant for iOS8 and above.
- The user will only get a prompt dialog for the first time. Later its only possible to change the setting via the notification center. - The user will only get a prompt dialog for the first time. Later its only possible to change the setting via the notification center.
```javascript ```javascript
window.plugin.notification.local.registerPermission(); window.plugin.notification.local.registerPermission(function (granted) {
// console.log('Permission has been granted: ' + granted);
});
``` ```
### Schedule local notifications ### Schedule local notifications
......
...@@ -39,6 +39,9 @@ ...@@ -39,6 +39,9 @@
<header-file src="src/ios/APPLocalNotificationOptions.h" /> <header-file src="src/ios/APPLocalNotificationOptions.h" />
<source-file src="src/ios/APPLocalNotificationOptions.m" /> <source-file src="src/ios/APPLocalNotificationOptions.m" />
<header-file src="src/ios/AppDelegate+APPLocalNotification.h" />
<header-file src="src/ios/AppDelegate+APPLocalNotification.m" />
<source-file src="src/ios/UIApplication+APPLocalNotification.h" /> <source-file src="src/ios/UIApplication+APPLocalNotification.h" />
<source-file src="src/ios/UIApplication+APPLocalNotification.m" /> <source-file src="src/ios/UIApplication+APPLocalNotification.m" />
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#import "APPLocalNotification.h" #import "APPLocalNotification.h"
#import "APPLocalNotificationOptions.h" #import "APPLocalNotificationOptions.h"
#import "AppDelegate+APPLocalNotification.h"
#import "UIApplication+APPLocalNotification.h" #import "UIApplication+APPLocalNotification.h"
#import "UILocalNotification+APPLocalNotification.h" #import "UILocalNotification+APPLocalNotification.h"
...@@ -34,6 +35,8 @@ ...@@ -34,6 +35,8 @@
@property (readwrite, assign) BOOL deviceready; @property (readwrite, assign) BOOL deviceready;
// Event queue // Event queue
@property (readonly, nonatomic, retain) NSMutableArray* eventQueue; @property (readonly, nonatomic, retain) NSMutableArray* eventQueue;
// Needed when calling `registerPermission`
@property (nonatomic, retain) CDVInvokedUrlCommand* command;
@end @end
...@@ -216,7 +219,7 @@ ...@@ -216,7 +219,7 @@
* Inform if the app has the permission to show * Inform if the app has the permission to show
* badges and local notifications. * badges and local notifications.
*/ */
- (void) hasPermission:(CDVInvokedUrlCommand *)command - (void) hasPermission:(CDVInvokedUrlCommand*)command
{ {
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
CDVPluginResult* result; CDVPluginResult* result;
...@@ -236,12 +239,19 @@ ...@@ -236,12 +239,19 @@
/** /**
* Ask for permission to show badges. * Ask for permission to show badges.
*/ */
- (void) registerPermission:(CDVInvokedUrlCommand *)command - (void) registerPermission:(CDVInvokedUrlCommand*)command
{ {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
_command = command;
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
[[UIApplication sharedApplication] [[UIApplication sharedApplication]
registerPermissionToScheduleLocalNotifications]; registerPermissionToScheduleLocalNotifications];
}]; }];
#else
[self hasPermission:command];
#endif
} }
#pragma mark - #pragma mark -
...@@ -388,6 +398,18 @@ ...@@ -388,6 +398,18 @@
} }
} }
/**
* Called on otification settings registration is completed.
*/
- (void) didRegisterUserNotificationSettings:(UIUserNotificationSettings*)settings
{
if (_command)
{
[self hasPermission:_command];
_command = NULL;
}
}
#pragma mark - #pragma mark -
#pragma mark Life Cycle #pragma mark Life Cycle
...@@ -398,22 +420,25 @@ ...@@ -398,22 +420,25 @@
*/ */
- (void) pluginInitialize - (void) pluginInitialize
{ {
NSNotificationCenter* notificationCenter; NSNotificationCenter* center = [NSNotificationCenter
notificationCenter = [NSNotificationCenter
defaultCenter]; defaultCenter];
eventQueue = [[NSMutableArray alloc] init]; eventQueue = [[NSMutableArray alloc] init];
[notificationCenter addObserver:self [center addObserver:self
selector:@selector(didReceiveLocalNotification:) selector:@selector(didReceiveLocalNotification:)
name:CDVLocalNotification name:CDVLocalNotification
object:nil]; object:nil];
[notificationCenter addObserver:self [center addObserver:self
selector:@selector(didFinishLaunchingWithOptions:) selector:@selector(didFinishLaunchingWithOptions:)
name:UIApplicationDidFinishLaunchingNotification name:UIApplicationDidFinishLaunchingNotification
object:nil]; object:nil];
[center addObserver:self
selector:@selector(didRegisterUserNotificationSettings:)
name:UIApplicationRegisterUserNotificationSettings
object:nil];
} }
/** /**
......
/*
Copyright 2013-2014 appPlant UG
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "AppDelegate.h"
#import <Availability.h>
extern NSString* const UIApplicationRegisterUserNotificationSettings;
@interface AppDelegate (APPLocalNotification)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
// Tells the delegate what types of notifications may be used
- (void) application:(UIApplication*)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings*)settings;
#endif
@end
/*
Copyright 2013-2014 appPlant UG
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "AppDelegate+APPLocalNotification.h"
#import <Availability.h>
NSString* const UIApplicationRegisterUserNotificationSettings = @"UIApplicationRegisterUserNotificationSettings";
@implementation AppDelegate (APPLocalNotification)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
/**
* Tells the delegate what types of notifications may be used
* to get the user’s attention.
*/
- (void) application:(UIApplication*)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings*)settings
{
NSNotificationCenter* center = [NSNotificationCenter
defaultCenter];
// re-post (broadcast)
[center postNotificationName:UIApplicationRegisterUserNotificationSettings
object:settings];
}
#endif
@end
...@@ -265,10 +265,14 @@ exports.hasPermission = function (callback, scope) { ...@@ -265,10 +265,14 @@ exports.hasPermission = function (callback, scope) {
* The callback function's scope * The callback function's scope
*/ */
exports.registerPermission = function (callback, scope) { exports.registerPermission = function (callback, scope) {
if (device.platform != 'iOS') var fn = this.createCallbackFn(callback, scope);
if (device.platform != 'iOS') {
fn(true);
return; return;
}
exec(null, null, 'LocalNotification', 'registerPermission', []); exec(fn, null, 'LocalNotification', 'registerPermission', []);
}; };
exports.promptForPermission = function (callback, scope) { exports.promptForPermission = function (callback, scope) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment