行文环境
- Xcode 10.1
- Swfit 4.1
- iOS 12
- 阿里云
证书设置
证书配置分为开发环境和生产环境,需要与业务服务器的开发环境(如dev/test)和正式环境区分开。例如给测试分发的Ad Hoc包使用生产环境的证书,但业务是test环境。
Ad Hoc包为什么收不到通知
因为Ad Hoc使用生产环境证书,设备的deviceToken在不同证书环境是不同的,但测试包的业务是test环境,后台会在test环境使用开发的apns设置,导致deviceToken无法匹配到对应的设备。
payload变化
老版本payload,只有body1
2
3
4
5
6
7
8
9
10
11{
"aps": {
"alert": {
"your notification body",
},
"badge": 1,
"sound": "default",
},
"key1":"value1",
"key2":"value2"
}
新版payload,新增title、subtitle、body、category等字段1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"aps": {
"alert": {
"title": "title",
"subtitle": "subtitle",
"body": "body"
},
"badge": 1,
"sound": "default",
"category": "test_category",
"mutable-content": 1
},
"key1":"value1",
"key2":"value2"
}
业务逻辑字段方式没有变化,直接加在json里
通知实现
初始化SDK
使用配置文件AliyunEmasServices-Info.plist
直接调用autoInit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17func setup(_ application: UIApplication, launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
CloudPushSDK.autoInit { result in
guard let result = result else {
log.error("Push SDK init failed, error: result is nil!")
return
}
if result.success {
log.debug("Push SDK init success, deviceId: \(String(describing: CloudPushSDK.getDeviceId()))")
} else {
log.error("Push SDK init failed, error: \(String(describing: result.error))")
}
}
//...
// 点击通知将App从关闭状态启动时,将通知打开回执上报
CloudPushSDK.sendNotificationAck(launchOptions)
}
请求通知权限并注册远程通知
第一次安装会弹出请求通知的alert1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21func registerAPNS(application: UIApplication) {
let center = UNUserNotificationCenter.current()
center.delegate = self
var options: UNAuthorizationOptions = [.alert, .sound, .badge]
if #available(iOS 12.0, *) {
options = [.alert, .sound, .badge, .providesAppNotificationSettings]
}
center.requestAuthorization(options: options) { (granted, error) in
if granted {
log.debug("User authored notification.")
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
} else {
log.debug("User denied notification.")
}
if let error = error {
log.error(error)
}
}
}
注册设备并上报deviceToken
1 | func registerDevice(_ deviceToken: Data) { |
注册通知类别
通知类别(category)用于给通知分类,可添加按钮或自定义UI1
2
3
4
5
6
7func createCustomNotificationCategory() {
let action = UNNotificationAction(identifier: "actionID", title: "buttonTitle", options: [])
let category = UNNotificationCategory(identifier: "CategoryID", actions: [action],
intentIdentifiers: [],
options: .customDismissAction)
UNUserNotificationCenter.current().setNotificationCategories([category])
}
UNUserNotificationCenterDelegate
UNUserNotificationCenterDelegate
代替了UIAppDelegate
的旧通知接收方法didReceiveRemoteNotification
将接收通知的情况分为app开启时(foreground)和app不在前台时(background)
1 | // app打开时调用 |