准备
- 需要苹果开发者账号
- 激活Apple push notification capbilities
- 一台真机iPhone
- 一个发送端,可以是服务器端,也可以是客户端如pusher(https://github.com/noodlewerk/NWPusher)
开始代码准备
首先需要在项目的APPDelegate.swift 中导入 UserNotifications framework (UserNotifications framework 是Apple 在 iOS 10 以后 推出的全新的推送框架提供全新的API和功能 关于UserNotifications 可以参考UserNotifications | Apple Developer Documentation)
step 1 :
导入UserNotifications 框架
import UserNotifications
step 2:
获取用户权限
封装一个方法 registerForPushNotifications() 处理用户权限的获取
func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
UIApplication.shared.registerForRemoteNotifications()
}
}
在application(_:didFinishLaunchingWithOptions:)
调用我们封装好的registerForPushNotifications()
registerForRemoteNotifications()方法 也就是为该设备注册通知服务,并且在注册成功后调用application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
step 3: 处理通知服务注册接结果
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print("Device Token: \(token)")
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
中如果使用自己的服务器后台或者第三方的推送服务,在这里上传DeviceToken给服务端(provider)
最后需要在Apple 开发者管理后台配置一下开发环境和生产环境的推送证书,如果有需要要把推送证书导出给provider
处理远程通知
UNUserNotificationCenterDelegate 是UserNotifications处理通知的代理方法;设置代理:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
registerForPushNotifications()
return true
}
实现代理方法:
optional public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void)
如下:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let aps = userInfo["aps"] as! [String: AnyObject]
// 处理通知
completionHandler()
}
}
参考
- Local and Remote Notification Programming Guide: APNs Overview
- Push Notifications Tutorial: Getting Started
- An Introduction to the UserNotifications Framework
- iOS 推送通知 功能简单实现 - 简书
- Local and Remote Notification Programming Guide: Communicating with APNs
- Establishing a Certificate-Based Connection to APNs | Apple Developer Documentation
网友评论