AppDelegateTemp.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "AppDelegate.h"
  8. #import <RCTJPushModule.h>
  9. #import <React/RCTBridge.h>
  10. #import <React/RCTBundleURLProvider.h>
  11. #import <React/RCTRootView.h>
  12. #ifdef NSFoundationVersionNumber_iOS_9_x_Max
  13. #import <UserNotifications/UserNotifications.h>
  14. #endif
  15. @interface AppDelegate ()<JPUSHRegisterDelegate>
  16. @end
  17. @implementation AppDelegate
  18. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  19. // JPush初始化配置 可以延时初始化 不再强制在此初始化,在js里可以直接调用init
  20. // [JPUSHService setupWithOption:launchOptions appKey:@"02c7f79c9248ecadf25140f7"
  21. // channel:@"dev" apsForProduction:YES];
  22. // APNS
  23. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  24. if (@available(iOS 12.0, *)) {
  25. entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  26. }
  27. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  28. // [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
  29. // // 自定义消息
  30. // NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  31. // [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
  32. // // 地理围栏
  33. // [JPUSHService registerLbsGeofenceDelegate:self withLaunchOptions:launchOptions];
  34. // ReactNative环境配置
  35. RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  36. RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
  37. moduleName:@"example"
  38. initialProperties:nil];
  39. rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  40. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  41. UIViewController *rootViewController = [UIViewController new];
  42. rootViewController.view = rootView;
  43. self.window.rootViewController = rootViewController;
  44. [self.window makeKeyAndVisible];
  45. return YES;
  46. }
  47. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  48. {
  49. #if DEBUG
  50. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  51. #else
  52. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  53. #endif
  54. }
  55. //************************************************JPush start************************************************
  56. //注册 APNS 成功并上报 DeviceToken
  57. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  58. [JPUSHService registerDeviceToken:deviceToken];
  59. }
  60. //iOS 7 APNS
  61. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  62. // iOS 10 以下 Required
  63. NSLog(@"iOS 7 APNS");
  64. [JPUSHService handleRemoteNotification:userInfo];
  65. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  66. completionHandler(UIBackgroundFetchResultNewData);
  67. }
  68. //iOS 10 前台收到消息
  69. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  70. NSDictionary * userInfo = notification.request.content.userInfo;
  71. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  72. // Apns
  73. NSLog(@"iOS 10 APNS 前台收到消息");
  74. [JPUSHService handleRemoteNotification:userInfo];
  75. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  76. }
  77. else {
  78. // 本地通知 todo
  79. NSLog(@"iOS 10 本地通知 前台收到消息");
  80. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  81. }
  82. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  83. completionHandler(UNNotificationPresentationOptionAlert);
  84. }
  85. //iOS 10 消息事件回调
  86. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  87. NSDictionary * userInfo = response.notification.request.content.userInfo;
  88. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  89. // Apns
  90. NSLog(@"iOS 10 APNS 消息事件回调");
  91. [JPUSHService handleRemoteNotification:userInfo];
  92. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  93. [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  94. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  95. }
  96. else {
  97. // 本地通知
  98. NSLog(@"iOS 10 本地通知 消息事件回调");
  99. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  100. [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  101. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  102. }
  103. // 系统要求执行这个方法
  104. completionHandler();
  105. }
  106. //自定义消息
  107. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  108. NSDictionary * userInfo = [notification userInfo];
  109. [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  110. }
  111. //************************************************JPush end************************************************
  112. @end