AppDelegate+JPush.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // AppDelegate+JPush.m
  3. // delegateExtention
  4. //
  5. // Created by 张庆贺 on 15/8/3.
  6. // Copyright (c) 2015年 JPush. All rights reserved.
  7. //
  8. #import "AppDelegate+JPush.h"
  9. #import "JPushPlugin.h"
  10. #import <objc/runtime.h>
  11. #import <AdSupport/AdSupport.h>
  12. #import <UserNotifications/UserNotifications.h>
  13. #import "JPushDefine.h"
  14. @implementation AppDelegate (JPush)
  15. +(void)load{
  16. Method origin1;
  17. Method swizzle1;
  18. origin1 = class_getInstanceMethod([self class],@selector(init));
  19. swizzle1 = class_getInstanceMethod([self class], @selector(init_plus));
  20. method_exchangeImplementations(origin1, swizzle1);
  21. }
  22. -(instancetype)init_plus{
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidLaunch:) name:UIApplicationDidFinishLaunchingNotification object:nil];
  24. return [self init_plus];
  25. }
  26. NSDictionary *_launchOptions;
  27. -(void)applicationDidLaunch:(NSNotification *)notification{
  28. if (!_jpushEventCache) {
  29. _jpushEventCache = @{}.mutableCopy;
  30. }
  31. [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
  32. NSDictionary *event = @{@"registrationId": registrationID?:@""};
  33. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_receiveRegistrationId jsString:[event toJsonString]];
  34. }];
  35. if (notification != nil &&
  36. [[UIDevice currentDevice].systemVersion floatValue] < 10.0) {// iOS 10 以后通过 openNotification 这个回调触发事件。
  37. if (notification.userInfo) {
  38. if ([notification.userInfo valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
  39. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification
  40. jsString:[[self jpushFormatAPNSDic: notification.userInfo[UIApplicationLaunchOptionsRemoteNotificationKey]] toJsonString]];
  41. }
  42. if ([notification.userInfo valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]) {
  43. UILocalNotification *localNotification = [notification.userInfo valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
  44. NSMutableDictionary *localNotificationEvent = @{}.mutableCopy;
  45. localNotificationEvent[@"content"] = localNotification.alertBody;
  46. localNotificationEvent[@"badge"] = @(localNotification.applicationIconBadgeNumber);
  47. localNotificationEvent[@"extras"] = localNotification.userInfo;
  48. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[localNotificationEvent toJsonString]];
  49. }
  50. }
  51. }
  52. [JPUSHService setDebugMode];
  53. NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfig_FileName ofType:@"plist"];
  54. NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  55. NSNumber *delay = [plistData valueForKey:JPushConfig_Delay];
  56. _launchOptions = notification.userInfo;
  57. if (![delay boolValue]) {
  58. [self startJPushSDK];
  59. }
  60. }
  61. -(void)startJPushSDK{
  62. [self registerForRemoteNotification];
  63. [JPushPlugin setupJPushSDK:_launchOptions];
  64. }
  65. - (void)jpushSDKDidLoginNotification {
  66. NSDictionary *event = @{@"registrationId": JPUSHService.registrationID};
  67. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_receiveRegistrationId jsString:[event toJsonString]];
  68. }
  69. - (NSMutableDictionary *)jpushFormatAPNSDic:(NSDictionary *)dic {
  70. NSMutableDictionary *extras = @{}.mutableCopy;
  71. for (NSString *key in dic) {
  72. if([key isEqualToString:@"_j_business"] ||
  73. [key isEqualToString:@"_j_msgid"] ||
  74. [key isEqualToString:@"_j_uid"] ||
  75. [key isEqualToString:@"actionIdentifier"] ||
  76. [key isEqualToString:@"aps"]) {
  77. continue;
  78. }
  79. extras[key] = dic[key];
  80. }
  81. NSMutableDictionary *formatDic = dic.mutableCopy;
  82. formatDic[@"extras"] = extras;
  83. return formatDic;
  84. }
  85. -(void)registerForRemoteNotification{
  86. if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
  87. #ifdef NSFoundationVersionNumber_iOS_9_x_Max
  88. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  89. entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
  90. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  91. #endif
  92. }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  93. //可以添加自定义categories
  94. [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
  95. UIUserNotificationTypeSound |
  96. UIUserNotificationTypeAlert)
  97. categories:nil];
  98. } else if([[UIDevice currentDevice].systemVersion floatValue] < 8.0){
  99. //categories 必须为nil
  100. [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
  101. UIRemoteNotificationTypeSound |
  102. UIRemoteNotificationTypeAlert)
  103. categories:nil];
  104. }
  105. }
  106. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  107. [JPUSHService registerDeviceToken:deviceToken];
  108. }
  109. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
  110. [JPUSHService handleRemoteNotification:userInfo];
  111. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]];
  112. }
  113. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
  114. [JPUSHService handleRemoteNotification:userInfo];
  115. NSString *eventName;
  116. switch ([UIApplication sharedApplication].applicationState) {
  117. case UIApplicationStateBackground:
  118. eventName = JPushDocumentEvent_BackgroundNotification;
  119. break;
  120. default:
  121. eventName = JPushDocumentEvent_ReceiveNotification;
  122. break;
  123. }
  124. [JPushPlugin fireDocumentEvent:eventName jsString:[[self jpushFormatAPNSDic:userInfo] toJsonString]];
  125. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  126. completionHandler(UIBackgroundFetchResultNewData);
  127. });
  128. }
  129. -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
  130. NSMutableDictionary *userInfo = @{}.mutableCopy;
  131. if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  132. userInfo = [self jpushFormatAPNSDic:notification.request.content.userInfo];
  133. } else {
  134. UNNotificationContent *content = notification.request.content;
  135. userInfo[@"content"] = content.body;
  136. userInfo[@"badge"] = content.badge;
  137. userInfo[@"extras"] = content.userInfo;
  138. userInfo[@"identifier"] = notification.request.identifier;
  139. }
  140. completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
  141. if ([userInfo[@"aps"][@"content-available"] isEqualToNumber:@(1)]) {// content-available 当用户开启后台推送是,防止触发两次事件
  142. return;
  143. }
  144. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]];
  145. }
  146. -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
  147. UNNotification *notification = response.notification;
  148. NSMutableDictionary *userInfo = @{}.mutableCopy;
  149. if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  150. userInfo = [self jpushFormatAPNSDic:notification.request.content.userInfo];
  151. } else {
  152. UNNotificationContent *content = notification.request.content;
  153. userInfo[@"content"] = content.body;
  154. userInfo[@"badge"] = content.badge;
  155. userInfo[@"extras"] = content.userInfo;
  156. userInfo[@"identifier"] = notification.request.identifier;
  157. }
  158. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[userInfo toJsonString]];
  159. completionHandler();
  160. }
  161. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  162. NSMutableDictionary *localNotificationEvent = @{}.mutableCopy;
  163. localNotificationEvent[@"content"] = notification.alertBody;
  164. localNotificationEvent[@"badge"] = @(notification.applicationIconBadgeNumber);
  165. localNotificationEvent[@"extras"] = notification.userInfo;
  166. [[NSNotificationCenter defaultCenter] postNotificationName:JPushDocumentEvent_ReceiveLocalNotification object:localNotificationEvent];
  167. }
  168. - (void)applicationWillEnterForeground:(UIApplication *)application {
  169. // [application setApplicationIconBadgeNumber:0];
  170. // [application cancelAllLocalNotifications];
  171. }
  172. - (void)applicationDidEnterBackground:(UIApplication *)application {
  173. // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  174. }
  175. @end