AppDelegate+JPush.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. -(void)fireOpenNotification:(NSTimer*)timer{
  27. if (SharedJPushPlugin) {
  28. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[timer.userInfo toJsonString]];
  29. [timer invalidate];
  30. }
  31. }
  32. NSDictionary *_launchOptions;
  33. -(void)applicationDidLaunch:(NSNotification *)notification{
  34. if (notification) {
  35. if (notification.userInfo) {
  36. NSDictionary *userInfo1 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  37. if (userInfo1.count > 0) {
  38. [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo1 repeats:YES];
  39. }
  40. NSDictionary *userInfo2 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
  41. if (userInfo2.count > 0) {
  42. [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo2 repeats:YES];
  43. }
  44. }
  45. [JPUSHService setDebugMode];
  46. NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfig_FileName ofType:@"plist"];
  47. NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  48. NSNumber *delay = [plistData valueForKey:JPushConfig_Delay];
  49. _launchOptions = notification.userInfo;
  50. if (![delay boolValue]) {
  51. [self startJPushSDK];
  52. }
  53. }
  54. }
  55. -(void)startJPushSDK{
  56. [self registerForRemoteNotification];
  57. [JPushPlugin setupJPushSDK:_launchOptions];
  58. }
  59. -(void)registerForRemoteNotification{
  60. if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
  61. #ifdef NSFoundationVersionNumber_iOS_9_x_Max
  62. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  63. entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
  64. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  65. #endif
  66. }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  67. //可以添加自定义categories
  68. [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
  69. UIUserNotificationTypeSound |
  70. UIUserNotificationTypeAlert)
  71. categories:nil];
  72. } else if([[UIDevice currentDevice].systemVersion floatValue] < 8.0){
  73. //categories 必须为nil
  74. [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
  75. UIRemoteNotificationTypeSound |
  76. UIRemoteNotificationTypeAlert)
  77. categories:nil];
  78. }
  79. }
  80. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  81. [JPUSHService registerDeviceToken:deviceToken];
  82. }
  83. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
  84. [JPUSHService handleRemoteNotification:userInfo];
  85. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]];
  86. }
  87. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
  88. [JPUSHService handleRemoteNotification:userInfo];
  89. NSString *eventName;
  90. switch ([UIApplication sharedApplication].applicationState) {
  91. case UIApplicationStateInactive:
  92. eventName = JPushDocumentEvent_OpenNotification;
  93. break;
  94. case UIApplicationStateActive:
  95. eventName = JPushDocumentEvent_ReceiveNotification;
  96. break;
  97. case UIApplicationStateBackground:
  98. eventName = JPushDocumentEvent_BackgroundNotification;
  99. break;
  100. default:
  101. break;
  102. }
  103. [JPushPlugin fireDocumentEvent:eventName jsString:[userInfo toJsonString]];
  104. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  105. completionHandler(UIBackgroundFetchResultNewData);
  106. });
  107. }
  108. -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
  109. NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:notification.request.content.userInfo];
  110. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]];
  111. completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
  112. }
  113. -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
  114. NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo];
  115. @try {
  116. [userInfo setValue:[response valueForKey:@"userText"] forKey:@"userText"];
  117. } @catch (NSException *exception) { }
  118. [userInfo setValue:response.actionIdentifier forKey:@"actionIdentifier"];
  119. [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[userInfo toJsonString]];
  120. completionHandler();
  121. }
  122. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  123. [[NSNotificationCenter defaultCenter] postNotificationName:JPushDocumentEvent_ReceiveLocalNotification object:notification.userInfo];
  124. }
  125. - (void)applicationWillEnterForeground:(UIApplication *)application {
  126. // [application setApplicationIconBadgeNumber:0];
  127. // [application cancelAllLocalNotifications];
  128. }
  129. - (void)applicationDidEnterBackground:(UIApplication *)application {
  130. // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  131. }
  132. @end