Skip to content

Commit ed15704

Browse files
authored
Merge pull request avivais#87 from pocketninja/ios-10-push-notifications
iOS 10 push notifications
2 parents 69fba55 + 7cdd7d6 commit ed15704

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<plugin xmlns="http://www.apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="parse-push-plugin" version="1.0.6">
3+
<plugin xmlns="http://www.apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="parse-push-plugin" version="1.0.7">
44
<name>ParsePushPlugin</name>
55
<description>Parse.Push plugin for phonegap/cordova/ionic framework</description>
66
<keywords>cordova,phonegap,ionic framework, parse server, parse push, push notification</keywords>

src/ios/ParsePushPlugin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#import <Cordova/CDV.h>
22
#import "AppDelegate.h"
3+
#import <UserNotifications/UserNotifications.h>
34

4-
@interface ParsePushPlugin: CDVPlugin
5+
@interface ParsePushPlugin: CDVPlugin <UNUserNotificationCenterDelegate>
56

67
@property bool hasRegistered;
78
@property (copy) NSString* callbackId;

src/ios/ParsePushPlugin.m

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,6 @@ - (void)getSubscriptions: (CDVInvokedUrlCommand *)command
6767

6868
- (void)subscribe: (CDVInvokedUrlCommand *)command
6969
{
70-
// Not sure if this is necessary
71-
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
72-
UIUserNotificationSettings *settings =
73-
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
74-
UIUserNotificationTypeBadge |
75-
UIUserNotificationTypeSound
76-
categories:nil];
77-
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
78-
[[UIApplication sharedApplication] registerForRemoteNotifications];
79-
}
80-
else {
81-
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
82-
UIRemoteNotificationTypeBadge |
83-
UIRemoteNotificationTypeAlert |
84-
UIRemoteNotificationTypeSound];
85-
}
86-
8770
CDVPluginResult* pluginResult = nil;
8871
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
8972
NSString *channel = [command.arguments objectAtIndex:0];
@@ -123,10 +106,28 @@ - (void)registerForPN {
123106

124107
if(!self.hasRegistered){
125108
NSLog(@"ParsePushPlugin is registering your device for PN");
126-
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
127-
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
128-
[application registerUserNotificationSettings:settings];
129-
[application registerForRemoteNotifications];
109+
110+
if (IsAtLeastiOSVersion(@"10.0")) {
111+
112+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
113+
center.delegate = self;
114+
115+
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
116+
if( !error ){
117+
[application registerForRemoteNotifications];
118+
}
119+
}];
120+
121+
}
122+
else if (IsAtLeastiOSVersion(@"8.0")) {
123+
124+
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
125+
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
126+
[application registerUserNotificationSettings:settings];
127+
[application registerForRemoteNotifications];
128+
}
129+
130+
}
130131

131132
self.hasRegistered = true;
132133
}
@@ -185,4 +186,13 @@ + (void)saveDeviceTokenToInstallation: (NSData*)deviceToken
185186
[currentInstallation saveInBackground];
186187
}
187188

189+
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
190+
NSLog(@"User info %@", notification.request.content.userInfo);
191+
completionHandler(UNNotificationPresentationOptionAlert);
192+
}
193+
194+
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
195+
NSLog(@"User info %@", response.notification.request.content.userInfo);
196+
}
197+
188198
@end

0 commit comments

Comments
 (0)