JPushKit

 

远程推送时 , 应用可能处于下列三种状态:

(1) . 应用开启时 , 应用在前台

(2) . 应用开启时 , 应用在后台

(3) . 应用未启动(应用被杀死)

从苹果APNS服务器远程推送时:

1 . 如果应用处于 (1) 状态 , 则不会发出声音 , 会直接调用appDelegate的代理方法didReceiveRemoteNotification,此时如果想收到类似系统的弹窗提示,则需要自定义弹窗,提示音,振动(弹窗可以参考 : ForeNotification (本地下载))

AudioServicesPlaySystemSound(1007);//系统提示音
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动

2 . 如果应用处于 (2) 状态 , 则会发出提示音, 点击推送消息 , 则会调用appDelegate的代理方法didReceiveRemoteNotification

3 . 如果应用处于 (3) 状态,则会发出提示音 , 点击推送消息 , 则会开启应用 , 在下面这个方法中会带上launchOptions这个参数,如果实现了application:didReceiveRemoteNotification:fetchCompletionHandler:这个方法,则会调用这个方法

 

静默推送

 

应用想收到静默推送需要满足的条件:

1.应用在前台/后台 (应用被杀死就收不到了)

2.应用实现了

application:didReceiveRemoteNotification:fetchCompletionHandler:

3.如果仅仅实现了application:didReceiveRemoteNotification:,没有实现application:didReceiveRemoteNotification:fetchCompletionHandler:,应用只有在前台时才能收到静默推送 , 应用在后台时,收不到静默推送

自定义消息推送

 

介绍: 极光推送提供了自定义消息推送 , 这种推送只有当应用在 前台 时才能收到 ; 当应用在 后台/被杀死 时,这时候的自定义消息被保存下来,直到应用处于前台时,应用才会收到.

使用场景: 当需要在前台处理大量数据的时候,可以使用自定义消息,例如应用某个模块需要更新了,这时候后台发送个自定义消息,等到应用启动了就可以自动去下载

总结:

1.应用在后台/前台/被杀死,都可以收到普通的远程推送

2.应用被杀死时,可以通过Background Fetch短时间唤醒应用

3.应用在后台/前台时,可以通过静默推送,修改一些数据

4.自定义消息/应用进入前台会收到通知

 

 

 

//
//  RAPushManager.swift
//  Radio
//
//  Created by baitongtong on 2021/3/5.
//  Copyright © 2021 hq. All rights reserved.
//
///喜欢关注,实时更新
import UIKit
RAPushManager: NSObject { static let shard = RAPushManager() /// 推送key private let Zi1TwTXBP3LZftGR = "********" /// 环境配置 private var isProd: Bool { develop_prefix == .prod } /// 今日推送提示 private let tipPush = "1BAHj0sPeRsxd4apgmi0" func initJPush(_ launchOptions: [UIApplication.LaunchOptionsKey: Any]?) { let entity = JPUSHRegisterEntity() entity.types = Int(Double(JPAuthorizationOptions.alert.rawValue) + TimeInterval(JPAuthorizationOptions.badge.rawValue) + Double(JPAuthorizationOptions.sound.rawValue)) JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self) JPUSHService.setup(withOption: launchOptions, appKey: Zi1TwTXBP3LZftGR, channel: "ios", apsForProduction: isProd) serAlias() setTag("IOS") if let noLaunchRemoteNotifi = launchOptions?[UIApplication.LaunchOptionsKey(rawValue: "UIApplicationLaunchOptionsRemoteNotificationKey")] { jpushNotificationCenterNoLaunchNotifiClick(noLaunchRemoteNotifi as? [String : Any]) } NotificationCenter.default.addObserver(self, selector: #selector(jpushNotificationCenterCuReceive), name: NSNotification.Name.jpfNetworkDidReceiveMessage, object: nil) resetBadgeNumber() } func pushDeInit() { JPUSHService.setAlias("", completion: { (c, a, o) in }, seq: 0) JPUSHService.setTags([], completion: { (c, s, o) in }, seq: 0) } /// 别名 func serAlias(_ alias: String = "\(user_id)") { JPUSHService.getAlias({ (c, a, o) in let alias = alias if c == 0 , a == alias { } else { JPUSHService.setAlias(alias, completion: { (code, alias, seq) in }, seq: 0) } }, seq: 0) } /// 打tag func setTag(_ tag: String) { JPUSHService.getAllTags({ (c, s, o) in if c == 0 , s?.contains(tag) == true { print(s ?? []) } else { /// let s0 = NSMutableSet(set: s ?? NSSet() as! Set<AnyHashable>) let s0 = NSMutableSet() s0.add(tag) JPUSHService.setTags(s0 as? Set<String>, completion: { (c, s, o) in }, seq: 0) } }, seq: 0) } /// 补充 /// 电话补充(可实现“推送不到短信到”的通知方式,提高推送达到率。) func setPhone(_ phone: String) { JPUSHService.setMobileNumber(phone) { (error) in if let error = error { print(error) } else { /// success print("绑定\(phone)Success") } } } /// 授权状态(检测此方法未授权,20秒后将提示打开通知) func isPushEnable() { JPUSHService.requestNotificationAuthorization {[self] (s) in switch s { case .statusDenied: if Date().ra.isTodayFirst(tipPush) { ra_after_async(20) { UIApplication.shared.keyWindow?.rootViewController?.showAlert(title: "消息中心", content: "接受通知可享受更好的体验,前往设置。", { gotoSet() }) } } default: break } } } /// 设置授权 func gotoSet() { JPUSHService.openSettings { (r) in } } } extension AppDelegate { func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { /// Required - 注册 DeviceToken JPUSHService.registerDeviceToken(deviceToken) } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { RALog(error) } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { RALog(userInfo) } } extension RAPushManager: JPUSHRegisterDelegate{ /// Authorization func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]!) { switch status { case .statusProvisional,.statusAuthorized: break default: break } } /// custom @objc func jpushNotificationCenterCuReceive(_ notification: Notification) { RALog(notification) } /// func jpushNotificationCenterNoLaunchNotifiClick(_ notification: [String: Any]?) { dataProcessing(notification) } /// iOS 10 Support 内 func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) { let userInfo = notification.request.content.userInfo if notification.request.trigger!.isKind(of: UNPushNotificationTrigger.self) { JPUSHService.handleRemoteNotification(userInfo) } /// 是否提醒用户 (Badge、Sound、Alert三种类型) completionHandler(Int(UNAuthorizationOptions.alert.rawValue)) // dataProcessing(userInfo) } /// iOS 10 Support func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) { let userInfo = response.notification.request.content.userInfo if response.notification.request.trigger!.isKind(of: UNPushNotificationTrigger.self) { JPUSHService.handleRemoteNotification(userInfo) } completionHandler(); // 系统要求执行这个方法 /// 启动 外 dataProcessing(userInfo) } /// iOS 12 Support func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification!) { if (notification != nil) && notification.request.trigger!.isKind(of: UNPushNotificationTrigger.self){ ///从通知界面直接进入应用 }else{ ///从通知设置界面进入应用 } } /// 清除角标 private func resetBadgeNumber() { UIApplication.shared.applicationIconBadgeNumber = 0 JPUSHService.setBadge(0) } } /// 业务逻辑处理 extension RAPushManager { func dataProcessing(_ notification: Any?) { /// “中央” 数据处理处 func process(_ dic: [String: Any]) { /// 处理Extras字段 ra_main_async { /// 跳转url if let url = dic["**"] as? String { } } } guard let notifi = notification as? [String: Any] else { return } process(notifi) RALog("收到推送内容:\(notifi)") } }

 

posted @ 2021-03-23 21:02  M·emor·Y  阅读(74)  评论(0编辑  收藏  举报