首先得配置一系列證書古毛,參考這篇文章用 Swift 實(shí)現(xiàn)通知推送的新手指南
這里得提一下,花了我很多時(shí)間的:必須保證鑰匙串中只有一個(gè)push的證書,可能是我測(cè)試中搞了好幾次,.p12文件一直沒有導(dǎo)出來(lái)汉操,另外沒法導(dǎo)出.p12文件的朋友請(qǐng)確定push證書在登錄模塊中,然后點(diǎn)我的證書蒙兰,選定證書導(dǎo)出磷瘤,如下圖
我用的推送服務(wù)器是這個(gè)
記得把代碼中的token復(fù)制到pusher中芒篷,然后選擇對(duì)應(yīng)的證書,如圖
結(jié)果如圖
////? AppDelegate.swift//? PushTest////? Created by 黃之信 on 17/3/13.//? Copyright ? 2017年 MichaelHuang. All rights reserved.//import UIKitimport UserNotifications@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {? ? var window: UIWindow?? ? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {? ? ? ? // Override point for customization after application launch.? ? ? ? registerAppNotificationSettings(launchOptions: launchOptions as [NSObject : AnyObject]?)? ? ? ? return true? ? }? ? private func registerAppNotificationSettings(launchOptions: [NSObject: AnyObject]?) {? ? ? ? if #available(iOS 10.0, *) {? ? ? ? ? ? let notifiCenter = UNUserNotificationCenter.current()? ? ? ? ? ? notifiCenter.delegate = self? ? ? ? ? ? let types = UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound])? ? ? ? ? ? notifiCenter.requestAuthorization(options: types) { (flag, error) in? ? ? ? ? ? ? ? if flag {? ? ? ? ? ? ? ? ? ? print("iOS request notification success")? ? ? ? ? ? ? ? }else{? ? ? ? ? ? ? ? ? ? print(" iOS 10 request notification fail")? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? } else { //iOS8,iOS9注冊(cè)通知//? ? ? ? ? ? let setting = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)//? ? ? ? ? ? UIApplication.sharedApplication().registerUserNotificationSettings(setting)? ? ? ? }? ? ? ? ? ? ? ? UIApplication.shared.registerForRemoteNotifications()? ? }? ? ? ? func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {? ? ? ? ? ? ? ? var token: String = ""? ? ? ? for i in 0..Void){
let userInfo = notification.request.content.userInfo
print("userInfo10:\(userInfo)")
completionHandler([.sound,.alert])
}
//iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void){
let userInfo = response.notification.request.content.userInfo
print("userInfo10:\(userInfo)")
completionHandler()
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}