mirror of
https://github.com/jpush/jpush-phonegap-plugin.git
synced 2025-01-19 13:52:49 +08:00
iOS - update support iOS 10
1.支持 iOS 10 设备推送。
This commit is contained in:
parent
116ac90492
commit
0342577830
@ -7,7 +7,9 @@
|
||||
//
|
||||
|
||||
#import "AppDelegate.h"
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
#import "JPUSHService.h"
|
||||
|
||||
@interface AppDelegate (JPush)
|
||||
|
||||
@interface AppDelegate (JPush) <JPUSHRegisterDelegate>
|
||||
-(void)registerForIos10RemoteNotification;
|
||||
@end
|
||||
|
@ -8,8 +8,11 @@
|
||||
|
||||
#import "AppDelegate+JPush.h"
|
||||
#import "JPushPlugin.h"
|
||||
#import "JPUSHService.h"
|
||||
#import <objc/runtime.h>
|
||||
#import <AdSupport/AdSupport.h>
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
|
||||
|
||||
|
||||
@implementation AppDelegate (JPush)
|
||||
|
||||
@ -24,12 +27,13 @@
|
||||
}
|
||||
|
||||
-(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];
|
||||
}
|
||||
|
||||
-(void)applicationDidLaunch:(NSNotification *)notification{
|
||||
if (notification) {
|
||||
[self registerForIos10RemoteNotification];
|
||||
[JPushPlugin setLaunchOptions:notification.userInfo];
|
||||
}
|
||||
}
|
||||
@ -62,4 +66,33 @@
|
||||
// [[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
|
||||
|
@ -9,6 +9,9 @@
|
||||
#import <Cordova/CDV.h>
|
||||
|
||||
#define kJPushPluginReceiveNotification @"JPushPluginReceiveNofication"
|
||||
#define kJPushPluginiOS10ForegroundReceiveNotification @"kJPushPluginiOS10ForegroundReceiveNotification"
|
||||
#define kJPushPluginiOS10ClickNotification @"kJPushPluginiOS10ClickNotification"
|
||||
|
||||
|
||||
@interface JPushPlugin : CDVPlugin{
|
||||
|
||||
@ -67,7 +70,7 @@
|
||||
* jpush.setTagsWithAlias 设置标签、别名完成
|
||||
* jpush.receiveMessage 收到自定义消息
|
||||
* jpush.receiveNotification 前台收到推送
|
||||
* jpush.backgoundNotification 后台收到推送
|
||||
* jpush.backgroundNotification 后台收到推送
|
||||
*/
|
||||
|
||||
@end
|
||||
|
@ -10,6 +10,8 @@
|
||||
#import "JPUSHService.h"
|
||||
#import <UIKit/UIKit.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_CHANNEL = @"CHANNEL";
|
||||
@ -18,6 +20,30 @@ static NSString *const JP_APP_ISIDFA = @"IsIDFA";
|
||||
static NSString *const JPushConfigFileName = @"PushConfig";
|
||||
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
|
||||
|
||||
#pragma mark- 外部接口
|
||||
@ -29,22 +55,6 @@ static NSDictionary *_launchOptions = nil;
|
||||
[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{
|
||||
NSNumber *result;
|
||||
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
|
||||
@ -92,19 +102,23 @@ static NSDictionary *_launchOptions = nil;
|
||||
name:kJPushPluginReceiveNotification
|
||||
object:nil];
|
||||
|
||||
if (_launchOptions) {
|
||||
NSDictionary *userInfo = [_launchOptions
|
||||
valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
|
||||
if ([userInfo count] >0) {
|
||||
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:kJPushPluginiOS10ForegroundReceiveNotification
|
||||
object:nil];
|
||||
|
||||
}
|
||||
[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{
|
||||
[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{
|
||||
@ -260,7 +281,22 @@ static NSDictionary *_launchOptions = nil;
|
||||
}
|
||||
|
||||
-(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];
|
||||
UIUserNotificationType type = settings.types;
|
||||
NSNumber *number = [NSNumber numberWithInteger:type];
|
||||
@ -270,7 +306,6 @@ static NSDictionary *_launchOptions = nil;
|
||||
NSNumber *number = [NSNumber numberWithInteger:type];
|
||||
[self handleResultWithValue:number command:command];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#pragma mark - 内部方法
|
||||
@ -294,11 +329,6 @@ static NSDictionary *_launchOptions = nil;
|
||||
NSNumber * isProduction = [plistData valueForKey:JP_APP_ISPRODUCTION];
|
||||
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;
|
||||
if(isIDFA){
|
||||
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
|
||||
-(void)handleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{
|
||||
CDVPluginResult *result = nil;
|
||||
@ -337,50 +385,35 @@ static NSDictionary *_launchOptions = nil;
|
||||
|
||||
#pragma mark 设置标签及别名回调
|
||||
-(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{
|
||||
|
||||
NSDictionary *dict = @{@"resultCode":[NSNumber numberWithInt:resultCode],
|
||||
@"tags" :tags == nil ? [NSNull null] : [tags allObjects],
|
||||
@"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(), ^{
|
||||
[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 {
|
||||
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(), ^{
|
||||
|
||||
[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;
|
||||
NSDictionary *userInfo = [notification object];
|
||||
|
||||
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
|
||||
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
|
||||
|
||||
switch ([UIApplication sharedApplication].applicationState) {
|
||||
case UIApplicationStateActive:{
|
||||
//前台收到
|
||||
@ -399,7 +432,7 @@ static NSDictionary *_launchOptions = nil;
|
||||
case UIApplicationStateBackground:{
|
||||
//后台收到
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user