1.Appdelegate文件中launchOptions方法中註冊通知api
func registerForPushNotifications(application: UIApplication) {app
if #available(iOS 10.0, *) {async
let center = UNUserNotificationCenter.current()spa
center.delegate = self //設置代理代理
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) inrem
guard granted else { return }get
self.getNotificationSettings() //獲取通知設置 string
}it
}else {io
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
}
func getNotificationSettings() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}else{
}
}
2.同時在launchOptions方法中(註冊後)設置APPkill後依舊能夠觸發跳轉功能
//open after killed
if #available(iOS 10.0, *) {
}else{
//open after killed
if let notification = launchOptions?[.remoteNotification] as? [String: AnyObject] {
guard notification["key"] != nil else{
return true
}
guard notification["value"] != nil else{
return true
}
let key = notification["key"] as! String
let value = notification["value"] as! String
switch key {
case "通知傳過來的key值":
handlePushForNavigationTo(key: key值)
break
case "通知傳過來的key值":
break
default:
break
}
}
3.跳轉頁面
func handlePushForNavigationTo(key:key值){
guard key值 > 0 else { return }
if let nextViewController =
ViewController.storyboardInstance() {
nextViewController.key值 = key值
nextViewController.isFromNotification = true //傳遞參數到跳轉頁面(以告知是從通知跳轉過來的,便於作不一樣界面處理)
let navController = UINavigationController(rootViewController: nextViewController)
var rootViewController = self.window?.rootViewController
//循環獲取到最上面的viewcontroller,不然沒法疊加跳轉通知頁面
while ((rootViewController?.presentedViewController) != nil) {
rootViewController = rootViewController?.presentedViewController
}
rootViewController?.present(
navController, animated: true, completion: nil)
}
}
4.通知代理方法,(APP未kill,點擊通知信息出發)
extension AppDelegate: UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let key = JSON(response.notification.request.content.userInfo)["key"].stringValue
print(key)
switch key {
case "key值":
//background
if let groupId =
JSON(response.notification.request.content.userInfo)["value"].int, key值 > 0{
handlePushForNavigationTo(key:key值)
}
break
default:
break
}
completionHandler()
}
//Called when a notification is delivered to a foreground app.
//Get push message when app is in foreground (show push messsage)
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("userNotificationCenter -> Get push message when app is in foreground",notification.request.content.userInfo)
print(JSON(notification.request.content.userInfo)["key"])
let key = JSON(notification.request.content.userInfo)["key"].stringValue
let value = JSON(notification.request.content.userInfo)["value"].numberValue
print(key)
print(value)
//print("after clicked notification view")
//handleGeneralPushNotification(notificationFromForegroundOrBackground: notification)
completionHandler([.alert, .badge, .sound])
}
}