在开始之前,首先了解下自定义消息和通知的几点区别。
自定义消息和通知的区别
收到推送自定义消息时推送通知栏不显示
自定义消息推送不经过APNS,所以说跟推送证书没有关系
只有app在前台时才能收到自定义消息,未启动或者启动但处于后台时接收不到(解决了不想在app未启动时收到推送消息的问题)
SDK初始化
Appdelegate文件
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 极光推送
if (UIDevice.current.systemVersion as NSString).floatValue >= 10.0 {
if #available(iOS 10.0, *) {
let entity = JPUSHRegisterEntity()
entity.types = NSInteger(UNAuthorizationOptions.alert.rawValue | UNAuthorizationOptions.badge.rawValue | UNAuthorizationOptions.sound.rawValue)
_ = JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
} else {
// Fallback on earlier versions
}
} else if (UIDevice.current.systemVersion as NSString).floatValue >= 8.0 {
//可以添加自定义categories
JPUSHService.register(forRemoteNotificationTypes: UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.alert.rawValue, categories: nil)
} else {
//categories 必须为nil
JPUSHService.register(forRemoteNotificationTypes: UIUserNotificationType.badge.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.alert.rawValue, categories: nil)
}
var isJpushProduction: Bool
#if DEBUG
isJpushProduction = false
#else
isJpushProduction = true
#endif
JPUSHService.setup(withOption: launchOptions, appKey: JPUSHService_appKey, channel: "Publish channel", apsForProduction: isJpushProduction, advertisingIdentifier: nil)
return true
}
通知(极光远程推送)
调用此 API 来取得应用程序对应的 RegistrationID。 只有当应用程序成功注册到 JPush 的服务器时才返回对应的值,否则返回空字符串。
func application(_ application: UIApplication, didRegisterFo