级别: ★★☆☆☆
标签:「iOS」「Swift 」「Push」
做者: dac_1033
审校: QiShare团队php
咱们以前发过关于推送的文章iOS 推送通知及通知扩展,其中介绍了推送相关流程及代码实现,不过使用OC实现的,如今咱们就来介绍一下在iOS10.0以上系统中,用Swift处理远程推送通知的相关流程及实现。git
苹果官方提供的远程推送通知的传递示意图以下:github
各关键组件之间的交互细节:bash
不论实现的语言是OC该是Swift,远程推送流程都是同样的,只是根据iOS版本的不一样,注册推送的方法、收到推送是的回调方法会有不一样。微信
// iOS10.0 以前:
{"aps":{"alert":{"body": "This is a message"},"sound":"default","badge":1}}
//iOS 10 基础Payload:
{"aps":{"alert":{"title":"I am title","subtitle":"I am subtitle","body":"I am body"},"sound":"default","badge":1}}
复制代码
//iOS8如下
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
//iOS8 - iOS10
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
复制代码
iOS10.0 及之后app
// OC
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert;
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
}
// Swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]{ granted, error in
}
复制代码
@available(iOS 10.0, *)
open class UNNotificationSettings : NSObject, NSCopying, NSSecureCoding {
open var authorizationStatus: UNAuthorizationStatus { get }
open var soundSetting: UNNotificationSetting { get }
open var badgeSetting: UNNotificationSetting { get }
open var alertSetting: UNNotificationSetting { get }
open var notificationCenterSetting: UNNotificationSetting { get }
open var lockScreenSetting: UNNotificationSetting { get }
open var carPlaySetting: UNNotificationSetting { get }
open var alertStyle: UNAlertStyle { get }
}
//获取设置
UNUserNotificationCenter.current().getNotificationSettings {
settings in
print(settings.authorizationStatus) // .authorized | .denied | .notDetermined
print(settings.badgeSetting) // .enabled | .disabled | .notSupported
}
复制代码
iOS10.0 及以后iOS加入了UserNotifications框架,Swift中的这个框架包括了如下库:框架
import UserNotifications.NSString_UserNotifications
import UserNotifications.UNError
import UserNotifications.UNNotification
import UserNotifications.UNNotificationAction
import UserNotifications.UNNotificationAttachment
import UserNotifications.UNNotificationCategory
import UserNotifications.UNNotificationContent
import UserNotifications.UNNotificationRequest
import UserNotifications.UNNotificationResponse
import UserNotifications.UNNotificationServiceExtension
import UserNotifications.UNNotificationSettings
import UserNotifications.UNNotificationSound
import UserNotifications.UNNotificationTrigger
import UserNotifications.UNUserNotificationCenter
复制代码
// 在AppDelegate的didFinishLaunchingWithOptions方法中注册远程推送通知
// - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
// 注册远程推送通知
func registerNotifications(_ application: UIApplication) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.getNotificationSettings { (setting) in
if setting.authorizationStatus == .notDetermined {
center.requestAuthorization(options: [.badge,.sound,.alert]) { (result, error) in
if(result){
if !(error != nil){
// 注册成功
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
} else{
//用户不容许推送
}
}
} else if (setting.authorizationStatus == .denied){
// 申请用户权限被拒
} else if (setting.authorizationStatus == .authorized){
// 用户已受权(再次获取dt)
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
} else {
// 未知错误
}
}
}
}
复制代码
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let dtDataStr = NSData.init(data: deviceToken)
let dtStr = dtDataStr.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")
// 上报deviceToken
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// 弹窗提示
}
复制代码
// UNUserNotificationCenterDelegate
// The method will be called on the delegate only if the application is in the foreground.
// If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented.
// The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list.
// This decision should be based on whether the information in the notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14);
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction.
// The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14) __TVOS_PROHIBITED;
// The method will be called on the delegate when the application is launched in response to the user's request to view in-app notification settings. // Add UNAuthorizationOptionProvidesAppNotificationSettings as an option in requestAuthorizationWithOptions:completionHandler: to add a button to inline notification settings view and the notification settings view in Settings. // The notification will be nil when opened from Settings. - (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification __IOS_AVAILABLE(12.0) __OSX_AVAILABLE(10.14) __WATCHOS_PROHIBITED __TVOS_PROHIBITED; 复制代码
写好代码并设置好证书后,可使用测试工具Pusher和真机测试一下...async
小编微信:可加并拉入《QiShare技术交流群》。ide
关注咱们的途径有:
QiShare(简书)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公众号)工具
推荐文章:
iOS UI状态保存和恢复(三)
iOS UI状态保存和恢复(二)
iOS UI状态保存和恢复(一)
Swift 运算符
iOS 中精肯定时的经常使用方法
Dart基础(一)
Dart基础(二)
Dart基础(三)
Dart基础(四)
奇舞周刊