前提准备:
1、工程准备
这里工程的配置已经基本完成
2、百度云推准备
百度云推创建账号和应用这里不再进行讲解,这里简单的说一下上传推送证书到百度云推平台上
pem文件简介:pem文件是服务器向苹果服务器做推送时候需要的文件,主要是给php向苹果服务器验证时使用
第一步:首先查看系统自带的openssl版本信息,打开控制台输入如下:openssl version 后回车会输出自带的openssl信息,因为百度云推现在仅支持版本为0.9.8zh的版本,如果不是次版本请先安装次版本
安装步骤如下
1、到https://www.openssl.org/source/old/0.9.x/ 下载安装包并解压
2、cd 你解压后的压缩包目录
3、./Configure darwin64-x86_64-cc --prefix=/usr/local/openssl --shared。如果此时执行此命令错误则需要我们到/usr/local/下创建一个openssl文件夹,然后再次执行此命令
4、make && make install 进行安装
5、/usr/local/openssl/bin/openssl version 查看opensell版本
生成pem文件步骤
1、在钥匙串中找到我们进行推送的推送证书并生成p12文件
2、使用我们安装的openssl版本进行创建pem文件
例如:/usr/local/openssl/bin/openssl pkcs12 -in myPush.p12 -out myPush.pem -nodes
最后使用生成的pem文件上传到百度云推上
代码例子如下:
注册远程推送通知与注册百度云推
//MARK:注册远程推送通知
func requestAuthorization(_ application: UIApplication) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
if granted == true {
application.registerForRemoteNotifications()
}
}
} else if #available(iOS 8.0, *) {
let types:UIUserNotificationType = [.badge , .alert , .sound]
let settings:UIUserNotificationSettings = UIUserNotificationSettings(types: types, categories: nil)
application.registerUserNotificationSettings(settings)
} else {
let types:UIRemoteNotificationType = [UIRemoteNotificationType.alert, UIRemoteNotificationType.badge, .sound]
application.registerForRemoteNotifications(matching: types)
}
}
/*注册百度推送*/
func registerBaiDuAuthorization(_ launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Void {
BPush.registerChannel(launchOptions, apiKey: "TNbj1VXPazd1ZDDf28WkUo6l", pushMode: .development, withFirstAction: "打开", withSecondAction: "回复", withCategory: "test", useBehaviorTextInput: true, isDebug: true)
let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
if userInfo != nil {
BPush.handleNotification(userInfo as! [AnyHashable : Any])
}
}
获取到token并向百度云推注册tookn
/*获取token并向百度推送注册*/
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Get Push token: \(deviceToken.hexString)")
BPush.registerDeviceToken(deviceToken)
BPush.bindChannel { (resoult, error) in
if error == nil {
return
}
if resoult != nil {
/*进行settag指定群组, listtag群组列表, deletetag删除群组, unbind解除绑定等操作*/
// BPush.setTag("test", withCompleteHandler: { (resoult, error) in
//
// })
}
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("注册远程token失败:\(error)")
}
处理通知时的调用方法
/*处理通知时的调用方法*/
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("应用程序状态state:\(application.applicationState)")
if application.applicationState == .active {
print("进入推送结果显示界面")
print("userInfo:\(userInfo)")
}
if application.applicationState == .background {
}
completionHandler(UIBackgroundFetchResult.newData)
}
推送成功后的回调处理
/*收到推送后的处理方法*/
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
BPush.handleNotification(userInfo)
}
ios8系统的注册推送的代理方法
/*iOS8系统调用方法*/
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}