一。 iOS 8以上
import NotificationCenter
//注册通知
if #available(iOS 8.0, *)
{
//通知类型
let uns = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
application.registerUserNotificationSettings(uns)
}
// 创建并添加本地通知
if #available(iOS 8.0, *)
{
//初始一个通知
let localNoti = UILocalNotification()
//通知上显示的内容
localNoti.alertBody = "通知上显示的提示内容"
//通知的触发时间
let firedate = Date(timeIntervalSinceNow: 5)
localNoti.fireDate = firedate
//待机界面的滑动动作提示
localNoti.alertAction = "打开应用"
localNoti.userInfo = ["local":"huanying"]
//应用程序图标右上角显示的消息数
localNoti.applicationIconBadgeNumber = 222
// 添加通知到系统队列中,系统会在指定的时间触发
UIApplication.shared.scheduleLocalNotification(localNoti)
}
二。iOS 10以上
import UserNotifications
实现代理 UNUserNotificationCenterDelegate
//注册通知
if #available(iOS 10, *)
{
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert,UNAuthorizationOptions.badge,UNAuthorizationOptions.sound], completionHandler: { (success, error) in
})
}
if #available(iOS 10, *){
UNUserNotificationCenter.current().delegate = self
}
//初始化通知
if #available(iOS 10, *)
{
//通知数据
let content = UNMutableNotificationContent()
content.title = "大标题"
content.subtitle = "xiaobiaoti"
content.body = "body"
content.userInfo = [:]
//时间闹钟
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: true)
//通知请求,制定文本和时间闹钟
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
//添加本地通知,系统会安排传递
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
@available(iOS 10.0, *)
//通知响应
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//获取通知数据,通过userInfo的内容 进行跳转等等
//response.notification.request.content.userInfo
completionHandler()
}
@available(iOS 10.0, *)
//在应用内展示通知
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
三。点击通知触发
1.// 应用在正在运行(在前台或后台运行),在didReceive代理方法中实现
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
let state = UIApplication.shared.applicationState
if state == .active{
}else if state == .background{
application.applicationIconBadgeNumber = 0
let info = notification.userInfo as! [String:String]
let str = info["local"]
let alert = UIAlertController(title: "欢迎", message: "打开", preferredStyle: .alert)
let action = UIAlertAction(title: "kai", style: .cancel, handler: nil)
alert.addAction(action)
window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
2.//应用程序关闭时,点击通知后的响应,在didFinishLaunchingWithOptions代理方法中实现
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//应用程序关闭时,点击通知后的响应
if launchOptions != nil{
let localNoti = launchOptions![UIApplicationLaunchOptionsKey.localNotification] as!UILocalNotification
localNoti.applicationIconBadgeNumber = 1
}
}
四。监控用户是否允许通知,并转到设置中,开启
override func viewDidAppear(_ animated: Bool) {
if UIApplication.shared.currentUserNotificationSettings?.types == UIUserNotificationType.init(rawValue: 0){
let alert = UIAlertController(title: "前往后台开启通知", message: "开启通知", preferredStyle: .alert)
let action = UIAlertAction(title: "开启", style: .default, handler: { (alert) in
let url = URL(string: "prefs:root=NOTIFICATIONS_ID")
if UIApplication.shared.canOpenURL(url!){
UIApplication.shared.openURL(url!)
}
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}