Merge pull request #276 from jpush/dev

Dev
This commit is contained in:
huangminlinux 2017-10-10 22:28:05 -05:00 committed by GitHub
commit 13341fd68a
3 changed files with 163 additions and 160 deletions

View File

@ -437,9 +437,7 @@ window.JPush.clearAllLocalNotifications()
监听 [jpush.receiveNotification](#前台收到推送)、[jpush.openNotification](点击推送通知),获取推送内容后,通过获取到的 `__JPUSHNotificationKey` 字段([本地通知](#本地通知) 设置的 `notificationID`)来判断是本地通知,并处理。 监听 [jpush.receiveNotification](#前台收到推送)、[jpush.openNotification](点击推送通知),获取推送内容后,通过获取到的 `__JPUSHNotificationKey` 字段([本地通知](#本地通知) 设置的 `notificationID`)来判断是本地通知,并处理。
### 点击本地通知横幅启动 App
监听 `jpush.startLocalNotification` 事件。
## 页面的统计 ## 页面的统计

View File

@ -1,156 +1,162 @@
// //
// AppDelegate+JPush.m // AppDelegate+JPush.m
// delegateExtention // delegateExtention
// //
// Created by on 15/8/3. // Created by on 15/8/3.
// Copyright (c) 2015 JPush. All rights reserved. // Copyright (c) 2015 JPush. All rights reserved.
// //
#import "AppDelegate+JPush.h" #import "AppDelegate+JPush.h"
#import "JPushPlugin.h" #import "JPushPlugin.h"
#import <objc/runtime.h> #import <objc/runtime.h>
#import <AdSupport/AdSupport.h> #import <AdSupport/AdSupport.h>
#import <UserNotifications/UserNotifications.h> #import <UserNotifications/UserNotifications.h>
#import "JPushDefine.h" #import "JPushDefine.h"
@implementation AppDelegate (JPush) @implementation AppDelegate (JPush)
+(void)load{ +(void)load{
Method origin1; Method origin1;
Method swizzle1; Method swizzle1;
origin1 = class_getInstanceMethod([self class],@selector(init)); origin1 = class_getInstanceMethod([self class],@selector(init));
swizzle1 = class_getInstanceMethod([self class], @selector(init_plus)); swizzle1 = class_getInstanceMethod([self class], @selector(init_plus));
method_exchangeImplementations(origin1, swizzle1); method_exchangeImplementations(origin1, swizzle1);
} }
-(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)fireOpenNotification:(NSTimer*)timer{ -(void)fireOpenNotification:(NSTimer*)timer{
if (SharedJPushPlugin) { if (SharedJPushPlugin) {
[JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[timer.userInfo toJsonString]]; [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[timer.userInfo toJsonString]];
[timer invalidate]; [timer invalidate];
} }
} }
NSDictionary *_launchOptions; NSDictionary *_launchOptions;
-(void)applicationDidLaunch:(NSNotification *)notification{ -(void)applicationDidLaunch:(NSNotification *)notification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jpushSDKDidLoginNotification) name:kJPFNetworkDidLoginNotification object:nil];
if (notification) {
if (notification.userInfo) { if (notification) {
NSDictionary *userInfo1 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (notification.userInfo) {
if (userInfo1.count > 0) { NSDictionary *userInfo1 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo1 repeats:YES]; if (userInfo1.count > 0) {
} [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo1 repeats:YES];
}
NSDictionary *userInfo2 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (userInfo2.count > 0) { NSDictionary *userInfo2 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo2 repeats:YES]; if (userInfo2.count > 0) {
} [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo2 repeats:YES];
} }
[JPUSHService setDebugMode]; }
[JPUSHService setDebugMode];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfig_FileName ofType:@"plist"];
NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfig_FileName ofType:@"plist"];
NSNumber *delay = [plistData valueForKey:JPushConfig_Delay]; NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSNumber *delay = [plistData valueForKey:JPushConfig_Delay];
_launchOptions = notification.userInfo;
_launchOptions = notification.userInfo;
if (![delay boolValue]) {
[self startJPushSDK]; if (![delay boolValue]) {
} [self startJPushSDK];
} }
} }
}
-(void)startJPushSDK{
[self registerForRemoteNotification]; -(void)startJPushSDK{
[JPushPlugin setupJPushSDK:_launchOptions]; [self registerForRemoteNotification];
} [JPushPlugin setupJPushSDK:_launchOptions];
}
-(void)registerForRemoteNotification{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { - (void)jpushSDKDidLoginNotification {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max NSDictionary *event = @{@"registrationId": JPUSHService.registrationID};
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; [JPushPlugin fireDocumentEvent:JPushDocumentEvent_receiveRegistrationId jsString:[event toJsonString]];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; }
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif -(void)registerForRemoteNotification{
}else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
//categories #ifdef NSFoundationVersionNumber_iOS_9_x_Max
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
UIUserNotificationTypeSound | entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
UIUserNotificationTypeAlert) [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
categories:nil]; #endif
} else if([[UIDevice currentDevice].systemVersion floatValue] < 8.0){ }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//categories nil //categories
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIRemoteNotificationTypeSound | UIUserNotificationTypeSound |
UIRemoteNotificationTypeAlert) UIUserNotificationTypeAlert)
categories:nil]; categories:nil];
} } else if([[UIDevice currentDevice].systemVersion floatValue] < 8.0){
} //categories nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { UIRemoteNotificationTypeSound |
[JPUSHService registerDeviceToken:deviceToken]; UIRemoteNotificationTypeAlert)
} categories:nil];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ }
[JPUSHService handleRemoteNotification:userInfo];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]]; [JPUSHService registerDeviceToken:deviceToken];
} }
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[JPUSHService handleRemoteNotification:userInfo]; [JPUSHService handleRemoteNotification:userInfo];
NSString *eventName;
switch ([UIApplication sharedApplication].applicationState) { [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]];
case UIApplicationStateInactive: }
eventName = JPushDocumentEvent_OpenNotification;
break; -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
case UIApplicationStateActive: [JPUSHService handleRemoteNotification:userInfo];
eventName = JPushDocumentEvent_ReceiveNotification; NSString *eventName;
break; switch ([UIApplication sharedApplication].applicationState) {
case UIApplicationStateBackground: case UIApplicationStateInactive:
eventName = JPushDocumentEvent_BackgroundNotification; eventName = JPushDocumentEvent_OpenNotification;
break; break;
default: case UIApplicationStateActive:
break; eventName = JPushDocumentEvent_ReceiveNotification;
} break;
[JPushPlugin fireDocumentEvent:eventName jsString:[userInfo toJsonString]]; case UIApplicationStateBackground:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ eventName = JPushDocumentEvent_BackgroundNotification;
completionHandler(UIBackgroundFetchResultNewData); break;
}); default:
} break;
}
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{ [JPushPlugin fireDocumentEvent:eventName jsString:[userInfo toJsonString]];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:notification.request.content.userInfo]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]]; completionHandler(UIBackgroundFetchResultNewData);
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); });
} }
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo]; NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:notification.request.content.userInfo];
@try { [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]];
[userInfo setValue:[response valueForKey:@"userText"] forKey:@"userText"]; completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
} @catch (NSException *exception) { } }
[userInfo setValue:response.actionIdentifier forKey:@"actionIdentifier"];
[JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[userInfo toJsonString]]; -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
completionHandler(); NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo];
} @try {
[userInfo setValue:[response valueForKey:@"userText"] forKey:@"userText"];
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { } @catch (NSException *exception) { }
[[NSNotificationCenter defaultCenter] postNotificationName:JPushDocumentEvent_ReceiveLocalNotification object:notification.userInfo]; [userInfo setValue:response.actionIdentifier forKey:@"actionIdentifier"];
} [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[userInfo toJsonString]];
completionHandler();
- (void)applicationWillEnterForeground:(UIApplication *)application { }
// [application setApplicationIconBadgeNumber:0];
// [application cancelAllLocalNotifications]; - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
} [[NSNotificationCenter defaultCenter] postNotificationName:JPushDocumentEvent_ReceiveLocalNotification object:notification.userInfo];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; - (void)applicationWillEnterForeground:(UIApplication *)application {
} // [application setApplicationIconBadgeNumber:0];
// [application cancelAllLocalNotifications];
@end }
- (void)applicationDidEnterBackground:(UIApplication *)application {
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
@end

View File

@ -27,7 +27,6 @@ static NSString *const JPushDocumentEvent_OpenNotification = @"openNoti
static NSString *const JPushDocumentEvent_BackgroundNotification = @"backgroundNotification"; static NSString *const JPushDocumentEvent_BackgroundNotification = @"backgroundNotification";
static NSString *const JPushDocumentEvent_SetTagsWithAlias = @"setTagsWithAlias"; static NSString *const JPushDocumentEvent_SetTagsWithAlias = @"setTagsWithAlias";
static NSString *const JPushDocumentEvent_ReceiveMessage = @"receiveMessage"; static NSString *const JPushDocumentEvent_ReceiveMessage = @"receiveMessage";
static NSString *const JPushDocumentEvent_StartLocalNotification = @"startLocalNotification";
static NSString *const JPushDocumentEvent_ReceiveLocalNotification = @"receiveLocalNotification"; static NSString *const JPushDocumentEvent_ReceiveLocalNotification = @"receiveLocalNotification";
static NSString *const JPushDocumentEvent_receiveRegistrationId = @"receiveRegistrationId";