首先得配置一系列证书,参考这篇文章用 Swift 实现通知推送的新手指南
这里得提一下,花了我很多时间的:必须保证钥匙串中只有一个push的证书,可能是我测试中搞了好几次,.p12文件一直没有导出来,另外没法导出.p12文件的朋友请确定push证书在登录模块中,然后点我的证书,选定证书导出,如下图
我用的推送服务器是这个
记得把代码中的token复制到pusher中,然后选择对应的证书,如图
结果如图
//// 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注册通知// 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新增:处理后台点击通知的代理方法
@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:.
}
}