iOS - update support iOS 10

1.支持 iOS 10 设备推送。
This commit is contained in:
pikacode 2016-09-22 17:06:30 +08:00
parent 116ac90492
commit 0342577830
4 changed files with 137 additions and 66 deletions

View File

@ -7,7 +7,9 @@
// //
#import "AppDelegate.h" #import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>
#import "JPUSHService.h"
@interface AppDelegate (JPush) @interface AppDelegate (JPush) <JPUSHRegisterDelegate>
-(void)registerForIos10RemoteNotification;
@end @end

View File

@ -8,8 +8,11 @@
#import "AppDelegate+JPush.h" #import "AppDelegate+JPush.h"
#import "JPushPlugin.h" #import "JPushPlugin.h"
#import "JPUSHService.h"
#import <objc/runtime.h> #import <objc/runtime.h>
#import <AdSupport/AdSupport.h>
#import <UserNotifications/UserNotifications.h>
@implementation AppDelegate (JPush) @implementation AppDelegate (JPush)
@ -24,12 +27,13 @@
} }
-(instancetype)init_plus{ -(instancetype)init_plus{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidLaunch:) name:@"UIApplicationDidFinishLaunchingNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidLaunch:) name:UIApplicationDidFinishLaunchingNotification object:nil];
return [self init_plus]; return [self init_plus];
} }
-(void)applicationDidLaunch:(NSNotification *)notification{ -(void)applicationDidLaunch:(NSNotification *)notification{
if (notification) { if (notification) {
[self registerForIos10RemoteNotification];
[JPushPlugin setLaunchOptions:notification.userInfo]; [JPushPlugin setLaunchOptions:notification.userInfo];
} }
} }
@ -62,4 +66,33 @@
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
} }
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:notification.request.content.userInfo];
[userInfo setValue:kJPushPluginiOS10ForegroundReceiveNotification forKey:@"JPushNotificationType"];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginiOS10ForegroundReceiveNotification object:userInfo];
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo];
[userInfo setValue:kJPushPluginiOS10ClickNotification forKey:@"JPushNotificationType"];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPushPluginiOS10ClickNotification object:userInfo];
completionHandler();
}
-(void)registerForIos10RemoteNotification{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
}
}
@end @end

View File

@ -9,6 +9,9 @@
#import <Cordova/CDV.h> #import <Cordova/CDV.h>
#define kJPushPluginReceiveNotification @"JPushPluginReceiveNofication" #define kJPushPluginReceiveNotification @"JPushPluginReceiveNofication"
#define kJPushPluginiOS10ForegroundReceiveNotification @"kJPushPluginiOS10ForegroundReceiveNotification"
#define kJPushPluginiOS10ClickNotification @"kJPushPluginiOS10ClickNotification"
@interface JPushPlugin : CDVPlugin{ @interface JPushPlugin : CDVPlugin{
@ -67,7 +70,7 @@
* jpush.setTagsWithAlias * jpush.setTagsWithAlias
* jpush.receiveMessage * jpush.receiveMessage
* jpush.receiveNotification * jpush.receiveNotification
* jpush.backgoundNotification * jpush.backgroundNotification
*/ */
@end @end

View File

@ -10,6 +10,8 @@
#import "JPUSHService.h" #import "JPUSHService.h"
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <AdSupport/AdSupport.h> #import <AdSupport/AdSupport.h>
#import <UserNotifications/UserNotifications.h>
#import "AppDelegate+JPush.h"
static NSString *const JP_APP_KEY = @"APP_KEY"; static NSString *const JP_APP_KEY = @"APP_KEY";
static NSString *const JP_APP_CHANNEL = @"CHANNEL"; static NSString *const JP_APP_CHANNEL = @"CHANNEL";
@ -18,6 +20,30 @@ static NSString *const JP_APP_ISIDFA = @"IsIDFA";
static NSString *const JPushConfigFileName = @"PushConfig"; static NSString *const JPushConfigFileName = @"PushConfig";
static NSDictionary *_launchOptions = nil; static NSDictionary *_launchOptions = nil;
#define WEAK_SELF(weakSelf) __weak __typeof(&*self)weakSelf = self;
@implementation NSDictionary (JPush)
-(NSString*)toJsonString{
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
return jsonString;
}
@end
@implementation NSString (JPush)
-(NSDictionary*)toDictionary{
NSError *error;
NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
return dict;
}
@end
@interface JPushPlugin()
@end
@implementation JPushPlugin @implementation JPushPlugin
#pragma mark- #pragma mark-
@ -29,22 +55,6 @@ static NSDictionary *_launchOptions = nil;
[JPushPlugin registerForRemoteNotification]; [JPushPlugin registerForRemoteNotification];
} }
+(void)registerForRemoteNotification{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
}
-(void)isPushStopped:(CDVInvokedUrlCommand*)command{ -(void)isPushStopped:(CDVInvokedUrlCommand*)command{
NSNumber *result; NSNumber *result;
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) { if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
@ -92,19 +102,23 @@ static NSDictionary *_launchOptions = nil;
name:kJPushPluginReceiveNotification name:kJPushPluginReceiveNotification
object:nil]; object:nil];
if (_launchOptions) { [defaultCenter addObserver:self
NSDictionary *userInfo = [_launchOptions selector:@selector(networkDidReceiveNotification:)
valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; name:kJPushPluginiOS10ForegroundReceiveNotification
if ([userInfo count] >0) { object:nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",jsonString]];
});
} [defaultCenter addObserver:self
selector:@selector(networkDidReceiveNotification:)
name:kJPushPluginiOS10ClickNotification
object:nil];
if (_launchOptions) {
NSDictionary *userInfo = [_launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if ([userInfo count] >0) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.openNotification',%@)",[userInfo toJsonString]]];
});
} }
} }
@ -248,7 +262,14 @@ static NSDictionary *_launchOptions = nil;
} }
-(void)deleteLocalNotificationWithIdentifierKey:(CDVInvokedUrlCommand*)command{ -(void)deleteLocalNotificationWithIdentifierKey:(CDVInvokedUrlCommand*)command{
[JPUSHService deleteLocalNotificationWithIdentifierKey:(NSString*)command.arguments[0]]; NSString *identifier = [command argumentAtIndex:0];
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
JPushNotificationIdentifier *jpid = [JPushNotificationIdentifier new];
jpid.identifiers = @[identifier];
[JPUSHService removeNotification:jpid];
}else{
[JPUSHService deleteLocalNotificationWithIdentifierKey:identifier];
}
} }
-(void)clearAllLocalNotifications:(CDVInvokedUrlCommand*)command{ -(void)clearAllLocalNotifications:(CDVInvokedUrlCommand*)command{
@ -260,7 +281,22 @@ static NSDictionary *_launchOptions = nil;
} }
-(void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command{ -(void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
WEAK_SELF(weakSelf);
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"authorizationStatus"] = @(settings.authorizationStatus);
dict[@"soundSetting"] = @(settings.soundSetting);
dict[@"badgeSetting"] = @(settings.badgeSetting);
dict[@"alertSetting"] = @(settings.alertSetting);
dict[@"notificationCenterSetting"] = @(settings.notificationCenterSetting);
dict[@"lockScreenSetting"] = @(settings.lockScreenSetting);
dict[@"carPlaySetting"] = @(settings.carPlaySetting);
dict[@"alertStyle"] = @(settings.alertStyle);
[weakSelf handleResultWithValue:dict command:command];
}];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationType type = settings.types; UIUserNotificationType type = settings.types;
NSNumber *number = [NSNumber numberWithInteger:type]; NSNumber *number = [NSNumber numberWithInteger:type];
@ -270,7 +306,6 @@ static NSDictionary *_launchOptions = nil;
NSNumber *number = [NSNumber numberWithInteger:type]; NSNumber *number = [NSNumber numberWithInteger:type];
[self handleResultWithValue:number command:command]; [self handleResultWithValue:number command:command];
} }
} }
#pragma mark - #pragma mark -
@ -294,11 +329,6 @@ static NSDictionary *_launchOptions = nil;
NSNumber * isProduction = [plistData valueForKey:JP_APP_ISPRODUCTION]; NSNumber * isProduction = [plistData valueForKey:JP_APP_ISPRODUCTION];
NSNumber *isIDFA = [plistData valueForKey:JP_APP_ISIDFA]; NSNumber *isIDFA = [plistData valueForKey:JP_APP_ISIDFA];
if (!appkey || appkey.length == 0) {
NSLog(@"error: app key not found in PushConfig.plist ");
assert(0);
}
NSString *advertisingId = nil; NSString *advertisingId = nil;
if(isIDFA){ if(isIDFA){
advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
@ -311,6 +341,24 @@ static NSDictionary *_launchOptions = nil;
} }
+(void)registerForRemoteNotification{
[(AppDelegate*)[UIApplication sharedApplication].delegate registerForIos10RemoteNotification];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else if([[UIDevice currentDevice].systemVersion floatValue] < 8.0){
//categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
}
#pragma mark js #pragma mark js
-(void)handleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{ -(void)handleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{
CDVPluginResult *result = nil; CDVPluginResult *result = nil;
@ -337,50 +385,35 @@ static NSDictionary *_launchOptions = nil;
#pragma mark #pragma mark
-(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{ -(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{
NSDictionary *dict = @{@"resultCode":[NSNumber numberWithInt:resultCode], NSDictionary *dict = @{@"resultCode":[NSNumber numberWithInt:resultCode],
@"tags" :tags == nil ? [NSNull null] : [tags allObjects], @"tags" :tags == nil ? [NSNull null] : [tags allObjects],
@"alias" :alias == nil ? [NSNull null] : alias @"alias" :alias == nil ? [NSNull null] : alias
}; };
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.setTagsWithAlias',%@)",jsonString]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.setTagsWithAlias',%@)",[dict toJsonString]]];
}); });
} }
- (void)networkDidReceiveMessage:(NSNotification *)notification { - (void)networkDidReceiveMessage:(NSNotification *)notification {
if (notification) { if (notification) {
NSDictionary *userInfo = [notification userInfo];
//NSLog(@"%@",userInfo);
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
//NSLog(@"%@",jsonString);
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.receiveMessage',%@)",jsonString]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.receiveMessage',%@)",[notification.userInfo toJsonString]]];
[self.commandDelegate evalJs:[NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveMessageIniOSCallback('%@')",jsonString]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveMessageIniOSCallback('%@')",[notification.userInfo toJsonString]]];
}); });
} }
} }
-(void)networkDidReceiveNotification:(id)notification{ -(void)networkDidReceiveNotification:(NSNotification *)notification{
NSError *error; NSError *error;
NSDictionary *userInfo = [notification object]; NSDictionary *userInfo = [notification object];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
switch ([UIApplication sharedApplication].applicationState) { switch ([UIApplication sharedApplication].applicationState) {
case UIApplicationStateActive:{ case UIApplicationStateActive:{
// //
@ -399,7 +432,7 @@ static NSDictionary *_launchOptions = nil;
case UIApplicationStateBackground:{ case UIApplicationStateBackground:{
// //
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.backgroundNotification',%@)",jsonString]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.backgoundNotification',%@)",jsonString]];
}); });
break; break;
} }