Commit e38a8384 authored by Sebastián Katzer's avatar Sebastián Katzer
Browse files

RegisterPermission accept a callback

parent 0fd4ac32
Loading
Loading
Loading
Loading
+5 −2
Changes for README.md: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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
Changes for plugin.xml: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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" />

+30 −5
Changes for src/ios/APPLocalNotification.m: 30 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -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

@@ -238,10 +241,17 @@
 */
- (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
    NSNotificationCenter* center = [NSNotificationCenter
                                    defaultCenter];

    eventQueue = [[NSMutableArray alloc] init];

    [notificationCenter addObserver:self
    [center addObserver:self
               selector:@selector(didReceiveLocalNotification:)
                   name:CDVLocalNotification
                 object:nil];

    [notificationCenter addObserver:self
    [center addObserver:self
               selector:@selector(didFinishLaunchingWithOptions:)
                   name:UIApplicationDidFinishLaunchingNotification
                 object:nil];

    [center addObserver:self
               selector:@selector(didRegisterUserNotificationSettings:)
                   name:UIApplicationRegisterUserNotificationSettings
                 object:nil];
}

/**
+36 −0
Changes for src/ios/AppDelegate+APPLocalNotification.h: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 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
Changes for src/ios/AppDelegate+APPLocalNotification.m: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 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
Loading