1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import UIKit
extension NSNotification.Name { public static let jpush_notificationTypeOne: NSNotification.Name = NSNotification.Name(rawValue: "jpush_notificationTypeOne") }
class JPushNotificationManage: NSObject { static let share: JPushNotificationManage = JPushNotificationManage() private override init() {} enum JPushNotificationType: Int { case none = 0 } func register(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) { let entity = JPUSHRegisterEntity() entity.types = Int(JPAuthorizationOptions.alert.rawValue) | Int(JPAuthorizationOptions.badge.rawValue) | Int(JPAuthorizationOptions.sound.rawValue) JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self) JPUSHService.setup(withOption: launchOptions, appKey: kGData.jPushAppKey, channel: "App Store", apsForProduction:kGData.jPushIsProduction) JPUSHService.registrationIDCompletionHandler { code, registrationID in if code == 0 { dPrint("========= registrationID获取成功:\(registrationID ?? "")") } else { dPrint("========= registrationID获取失败") } } guard let options = launchOptions, let userInfo = options[.remoteNotification] as? [AnyHashable: Any] else { UIApplication.shared.applicationIconBadgeNumber = 0 return } JPUSHService.setBadge(0) DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) { [weak self] in self?.handleRemoteNotification(userInfo: userInfo, click: true) } } func registerDeviceToken(deviceToken: Data) { JPUSHService.registerDeviceToken(deviceToken) } func handleRemoteNotification(userInfo: [AnyHashable: Any], click: Bool) { guard let userInfo = userInfo as? [String: Any] else { return } print(userInfo) guard userInfo.count > 0 else { return } guard let typeStr = userInfo["extraMsgType"] as? String, let typeInt = Int(typeStr), let jtype = JPushNotificationType(rawValue: typeInt) else { return } guard click else {return} jumpToAnyPage(pageId: Int) } }
private extension JPushNotificationManage { }
extension JPushNotificationManage: JPUSHRegisterDelegate { func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]!) { } @available(iOS 10.0, *) func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification?) { } @available(iOS 10.0, *) func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) { let userInfo = notification.request.content.userInfo if notification.request.trigger is UNPushNotificationTrigger { JPUSHService.handleRemoteNotification(userInfo) handleRemoteNotification(userInfo: userInfo, click: false) } else { } completionHandler(Int(JPAuthorizationOptions.alert.rawValue) | Int(JPAuthorizationOptions.badge.rawValue) | Int(JPAuthorizationOptions.sound.rawValue)) } @available(iOS 10.0, *) func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) { let userInfo = response.notification.request.content.userInfo if response.notification.request.trigger is UNPushNotificationTrigger { haveReceivedRemoteNotification(userInfo: userInfo) JPUSHService.setBadge(0) } else { } completionHandler() } func haveReceivedRemoteNotification(userInfo: Dictionary<AnyHashable, Any>) { if userInfo.count == 0 { return } JPUSHService.handleRemoteNotification(userInfo) JPushNotificationManage.share.handleRemoteNotification(userInfo: userInfo, click: true) } }
|