|
| 1 | +import UIKit |
| 2 | +import React |
| 3 | +import React_RCTAppDelegate |
| 4 | +import ReactAppDependencyProvider |
| 5 | + |
| 6 | +@main |
| 7 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 8 | + var window: UIWindow? |
| 9 | + |
| 10 | + var reactNativeDelegate: ReactNativeDelegate? |
| 11 | + var reactNativeFactory: RCTReactNativeFactory? |
| 12 | + |
| 13 | + func application( |
| 14 | + _ application: UIApplication, |
| 15 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil |
| 16 | + ) -> Bool { |
| 17 | + let delegate = ReactNativeDelegate() |
| 18 | + let factory = RCTReactNativeFactory(delegate: delegate) |
| 19 | + delegate.dependencyProvider = RCTAppDependencyProvider() |
| 20 | + |
| 21 | + reactNativeDelegate = delegate |
| 22 | + reactNativeFactory = factory |
| 23 | + |
| 24 | + window = UIWindow(frame: UIScreen.main.bounds) |
| 25 | + |
| 26 | + factory.startReactNative( |
| 27 | + withModuleName: "HelloWord", |
| 28 | + in: window, |
| 29 | + launchOptions: launchOptions |
| 30 | + ) |
| 31 | + |
| 32 | + //************************************************ JPush Need************************************************ |
| 33 | + let entity = JPUSHRegisterEntity() |
| 34 | + entity.types = 0 |
| 35 | + JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self) |
| 36 | + |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + //************************************************ JPush Need************************************************ |
| 41 | + func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
| 42 | + // JPush 注册devicetoken |
| 43 | + JPUSHService.registerDeviceToken(deviceToken) |
| 44 | + } |
| 45 | + |
| 46 | + func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { |
| 47 | + // 注意调用 |
| 48 | + JPUSHService.handleRemoteNotification(userInfo) |
| 49 | + NotificationCenter.default.post(name: NSNotification.Name(J_APNS_NOTIFICATION_ARRIVED_EVENT), object: userInfo) |
| 50 | + completionHandler(.newData) |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate { |
| 56 | + override func sourceURL(for bridge: RCTBridge) -> URL? { |
| 57 | + self.bundleURL() |
| 58 | + } |
| 59 | + |
| 60 | + override func bundleURL() -> URL? { |
| 61 | +#if DEBUG |
| 62 | + RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index") |
| 63 | +#else |
| 64 | + Bundle.main.url(forResource: "main", withExtension: "jsbundle") |
| 65 | +#endif |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +//************************************************JPush Need ************************************************ |
| 71 | + |
| 72 | +extension AppDelegate:JPUSHRegisterDelegate { |
| 73 | + //MARK - JPUSHRegisterDelegate |
| 74 | + @available(iOS 10.0, *) |
| 75 | + func jpushNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, |
| 76 | + withCompletionHandler completionHandler: ((Int) -> Void)) { |
| 77 | + let userInfo = notification.request.content.userInfo |
| 78 | + |
| 79 | + if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true) { |
| 80 | + // 注意调用 |
| 81 | + JPUSHService.handleRemoteNotification(userInfo) |
| 82 | + NotificationCenter.default.post(name: NSNotification.Name(J_APNS_NOTIFICATION_ARRIVED_EVENT), object: userInfo) |
| 83 | + print("收到远程通知:\(userInfo)") |
| 84 | + } else { |
| 85 | + NotificationCenter.default.post(name: NSNotification.Name(J_LOCAL_NOTIFICATION_ARRIVED_EVENT), object: userInfo) |
| 86 | + print("收到本地通知:\(userInfo)") |
| 87 | + } |
| 88 | + |
| 89 | + completionHandler(Int(UNNotificationPresentationOptions.badge.rawValue | UNNotificationPresentationOptions.sound.rawValue | UNNotificationPresentationOptions.alert.rawValue)) |
| 90 | + } |
| 91 | + |
| 92 | + @available(iOS 10.0, *) |
| 93 | + func jpushNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: (() -> Void)) { |
| 94 | + |
| 95 | + let userInfo = response.notification.request.content.userInfo |
| 96 | + if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true) { |
| 97 | + // 注意调用 |
| 98 | + JPUSHService.handleRemoteNotification(userInfo) |
| 99 | + NotificationCenter.default.post(name: NSNotification.Name(J_APNS_NOTIFICATION_OPENED_EVENT), object: userInfo) |
| 100 | + print("点击远程通知:\(userInfo)") |
| 101 | + |
| 102 | + } else { |
| 103 | + print("点击本地通知:\(userInfo)") |
| 104 | + NotificationCenter.default.post(name: NSNotification.Name(J_LOCAL_NOTIFICATION_OPENED_EVENT), object: userInfo) |
| 105 | + } |
| 106 | + |
| 107 | + completionHandler() |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + func jpushNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification) { |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]?) { |
| 116 | + print("receive notification authorization status:\(status), info:\(String(describing: info))") |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + // //MARK - 自定义消息 |
| 121 | + func networkDidReceiveMessage(_ notification: NSNotification) { |
| 122 | + let userInfo = notification.userInfo! |
| 123 | + NotificationCenter.default.post(name: NSNotification.Name(J_CUSTOM_NOTIFICATION_EVENT), object: userInfo) |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +} |
0 commit comments