Forráskód Böngészése

RegisterPermission accept a callback

Sebastián Katzer 11 éve
szülő
commit
e38a8384f1

+ 5 - 2
README.md

@@ -126,14 +126,17 @@ window.plugin.notification.local.hasPermission(function (granted) {
 ```
 
 ### 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
 - 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.
 
 ```javascript
-window.plugin.notification.local.registerPermission();
+window.plugin.notification.local.registerPermission(function (granted) {
+    // console.log('Permission has been granted: ' + granted);
+});
 ```
 
 ### Schedule local notifications

+ 3 - 0
plugin.xml

@@ -39,6 +39,9 @@
         <header-file src="src/ios/APPLocalNotificationOptions.h" />
         <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.m" />
 

+ 39 - 14
src/ios/APPLocalNotification.m

@@ -21,6 +21,7 @@
 
 #import "APPLocalNotification.h"
 #import "APPLocalNotificationOptions.h"
+#import "AppDelegate+APPLocalNotification.h"
 #import "UIApplication+APPLocalNotification.h"
 #import "UILocalNotification+APPLocalNotification.h"
 
@@ -34,6 +35,8 @@
 @property (readwrite, assign) BOOL deviceready;
 // Event queue
 @property (readonly, nonatomic, retain) NSMutableArray* eventQueue;
+// Needed when calling `registerPermission`
+@property (nonatomic, retain) CDVInvokedUrlCommand* command;
 
 @end
 
@@ -216,7 +219,7 @@
  * Inform if the app has the permission to show
  * badges and local notifications.
  */
-- (void) hasPermission:(CDVInvokedUrlCommand *)command
+- (void) hasPermission:(CDVInvokedUrlCommand*)command
 {
     [self.commandDelegate runInBackground:^{
         CDVPluginResult* result;
@@ -236,12 +239,19 @@
 /**
  * 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:^{
         [[UIApplication sharedApplication]
          registerPermissionToScheduleLocalNotifications];
     }];
+#else
+    [self hasPermission:command];
+#endif
 }
 
 #pragma mark -
@@ -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 Life Cycle
 
@@ -398,22 +420,25 @@
  */
 - (void) pluginInitialize
 {
-    NSNotificationCenter* notificationCenter;
-
-    notificationCenter = [NSNotificationCenter
-                          defaultCenter];
+    NSNotificationCenter* center = [NSNotificationCenter
+                                    defaultCenter];
 
     eventQueue = [[NSMutableArray alloc] init];
 
-    [notificationCenter addObserver:self
-                           selector:@selector(didReceiveLocalNotification:)
-                               name:CDVLocalNotification
-                             object:nil];
+    [center addObserver:self
+               selector:@selector(didReceiveLocalNotification:)
+                   name:CDVLocalNotification
+                 object:nil];
+
+    [center addObserver:self
+               selector:@selector(didFinishLaunchingWithOptions:)
+                   name:UIApplicationDidFinishLaunchingNotification
+                 object:nil];
 
-    [notificationCenter addObserver:self
-                           selector:@selector(didFinishLaunchingWithOptions:)
-                               name:UIApplicationDidFinishLaunchingNotification
-                             object:nil];
+    [center addObserver:self
+               selector:@selector(didRegisterUserNotificationSettings:)
+                   name:UIApplicationRegisterUserNotificationSettings
+                 object:nil];
 }
 
 /**

+ 36 - 0
src/ios/AppDelegate+APPLocalNotification.h

@@ -0,0 +1,36 @@
+/*
+ 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

+ 47 - 0
src/ios/AppDelegate+APPLocalNotification.m

@@ -0,0 +1,47 @@
+/*
+ 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

+ 6 - 2
www/local-notification.js

@@ -265,10 +265,14 @@ exports.hasPermission = function (callback, scope) {
  *      The callback function's scope
  */
 exports.registerPermission = function (callback, scope) {
-    if (device.platform != 'iOS')
+    var fn = this.createCallbackFn(callback, scope);
+
+    if (device.platform != 'iOS') {
+        fn(true);
         return;
+    }
 
-    exec(null, null, 'LocalNotification', 'registerPermission', []);
+    exec(fn, null, 'LocalNotification', 'registerPermission', []);
 };
 
 exports.promptForPermission = function (callback, scope) {