ios 阿里消息推送流程总结
阿里消息推送流程总结:
最近公司项目接了一次阿里的推送,在此把详细的流程分享给大家,希望可以有此帮助,避免一些时间的消耗和一些入坑的几率
基本的业务流程:

一:创建应用,获取初始化所需的AppKey和Secret
1.登录阿里的移动研发平台的管理控制台,添加产品

2.点击添加应用,创建ios和android对应的平台

二:配置推送证书

1.获取CSR文件
1.1 在Mac电脑的应用程序中打开钥匙串访问,在顶部菜单栏中选择钥匙串访问>证书助理>从证书颁发机构请求证书

1.2 在弹出的证书信息中,输入邮箱地址,设置选择储存到磁盘,单击继续将CSR文件存储到本地

2. 生成AppID
2.1登录Apple Member Center,选择Certificates,Identifiers & Profiles选项

2.2 选择Identify,单击Identify右侧的+
2.3 选择App IDs 单击Continue。

2.4 配置Bundle ID等信息

2.5开启远程推送服务

3.创建生产和开发证书
3.1点击创建好的appid, 进入Edit your App ID Configuration页面,配置生产和开发环境下的证书

3.2 单击Choose File上传已获取到的CSR文件

3.3 生成后,download到本地
3.4 双击打开下载的开发环境和生产环境证书,系统会将其导入钥匙串中
3.5在Mac中打开钥匙串应用,选择登录>证书,分别右键导出开发环境和生产环境的.P12(配置相应的导入密码)证书文件

3.6 在阿里移动研发平台应用管理中推送配置中配置相应的开发和生产证书,上传成功后入下图所示,其中测试的token是指阿里sdk注册成功后返回的deviceId

三,sdk配置
1,集成SDK
一共有两种方法,手动添加的和Pod集成,这里我介绍的是pod集成的方法
1.1 在Podfile中添加source,指定Master仓库和阿里云仓库

1.2 在终端执行pod repo add命令,拉取阿里云Pod仓库到本地

2. 引入头文件
import CloudPushSDK
3. Objc配置
iOS端集成SDK时需要做-ObjC配置 ,即应用的 TARGETS -> Build Settings -> Linking -> Other Linker Flags ,需添加上-ObjC这个属性,否则推送服务无法正常使用 。

4. 打开系统通知

5. SDK初始化配置
5.1 SDK初始化
初始化sdk 成功后会返回所需的deviceId
CloudPushSDK.asyncInit("appkey", appSecret: "appSecret") { [self] (result) in
if ((result?.success) != nil){
print("deviceId == ",CloudPushSDK.getDeviceId() as Any)
self.registerAPNs(application: application)
CloudPushSDK.sendNotificationAck(options)
}else{
}
}
//打开debug日志
CloudPushSDK.turnOnDebug()
5.2 向苹果APNs注册获取deviceToken并上报到阿里云推送服务器
func registerAPNs(application: UIApplication) {
let center = UNUserNotificationCenter.current()
center .requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.badge,UNAuthorizationOptions.sound]) { (granted, error) in
if granted{
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
}
5.3 苹果推送注册成功回调,将苹果返回的deviceToken上传到CloudPush服务器
//register sucess block
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
CloudPushSDK .registerDevice(deviceToken) { (result) in
if ((result?.success) != nil){
print("Register deviceToken success")
}else{
print("Register deviceToken err== ",result?.error as Any)
}
}
}
5.4 苹果推送注册失败回调
//register failure
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error)
}
5.5 App处于启动状态时,通知打开回调(当有消息推送时,会调用此回调方法)
// App处于启动状态时,通知打开回调
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let cmd = userInfo["cmd"] {
NCRouterHelp.routerWithUrlString(urlString: cmd as! String, complete: nil)
}//设置本地的角标数为0
application.applicationIconBadgeNumber = 0;
//设置角标同步到服务器
CloudPushSDK.syncBadgeNum(0, withCallback: nil)
CloudPushSDK.sendNotificationAck(userInfo)
}
-----App处于前台收到通知
当App处于前台,有消息通知的时候 ,如需要在消息通知栏通知的时候,则:(需要提前导入import UserNotifications)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([UNNotificationPresentationOptions.sound, UNNotificationPresentationOptions.alert, UNNotificationPresentationOptions.badge])
}
已结束 good luck to you!