在AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarVC = TBTabBarController()
window!.rootViewController = tabBarVC
window!.makeKeyAndVisible()
let set = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(set)
// 程序被殺死時(shí)跳轉(zhuǎn)頁(yè)面
if let options = launchOptions {
if let notification = options[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification {
let userInfo = notification.userInfo
let apsInfo = userInfo?["id"] as? String
// 展示推送的信息
let alert = UIAlertView(title: "\(userInfo!)", message: nil, delegate: nil, cancelButtonTitle: "確定")
alert.show()
if apsInfo == "detail" {
//頁(yè)面跳轉(zhuǎn)
let VC = UIStoryboard(name: "Discover", bundle: nil).instantiateViewController(withIdentifier: "Message") as! MessageDetailViewController
VC.isForNotification = true
let nc = TBNavigationController(rootViewController: VC)
self.window?.rootViewController?.present(nc, animated: true, completion: nil)
}
}
}
return true
}
本地通知頁(yè)面跳轉(zhuǎn)
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
//程序后臺(tái)
if UIApplication.shared.applicationState == UIApplicationState.inactive {
guard let userInfos = notification.userInfo else {
return
}
let apsInfo = userInfos["id"] as? String
print(userInfos)
if apsInfo == "detail" {
let VC = UIStoryboard(name: "Discover", bundle: nil).instantiateViewController(withIdentifier: "Message") as! MessageDetailViewController
let nc = TBNavigationController(rootViewController: VC)
VC.isForNotification = true
self.window?.rootViewController?.present(nc, animated: true, completion: nil)
}
}
//程序前臺(tái)
if application.applicationState == UIApplicationState.active {
guard let userInfo = notification.userInfo else {
return
}
// let alert = UIAlertView(title: "\(userInfo)", message: nil, delegate: nil, cancelButtonTitle: "確定")
// alert.show()
}
}
遠(yuǎn)程推送
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
let tabBarVC = TBTabBarController()
window!.rootViewController = tabBarVC
// 程序進(jìn)入后臺(tái)
if UIApplication.shared.applicationState == UIApplicationState.inactive {
if TBUser.currentUser.logined {
let apsInfo = userInfo["aps"] as! NSDictionary
print(userInfo)
}
}
}
在viewController
let noti = UILocalNotification()
noti.repeatInterval = NSCalendar.Unit.minute
noti.fireDate = Date().addingTimeInterval(-1*60) // 每分鐘推送一次
noti.timeZone = NSTimeZone.default
noti.alertBody = "推送消息"
noti.alertTitle = "test"
noti.soundName = UILocalNotificationDefaultSoundName
noti.userInfo = ["id": "detail"]
noti.applicationIconBadgeNumber = 1
noti.alertAction = "跳轉(zhuǎn)"
UIApplication.shared.scheduleLocalNotification(noti)
移除所有通知
UIApplication.shared.cancelAllLocalNotifications()
有興趣的話 可以下載Demo: https://github.com/BJGX/LocalNotification