class: LocalNotification
本地通知
property
- requertIdentifier
String
,只读,创建通知的Identifier
func
sendLocalNotification(withIdentifier identifier:String,title:String? = nil, subtitle:String? = nil, body:String, bage:NSNumber = 1, tigger:UNNotificationTrigger, attachments:[UNNotificationAttachment]? = nil, sound:UNNotificationSound? = nil)
发送通知
createTimeIntervalTrigger(timeInterval:TimeInterval, repeats:Bool = false)
距离当前时间多少
TimeInterval
后触发,返回UNTimeIntervalNotificationTrigger
,repeats
==true
,timeInterval必须大于60createCalendarTrigger(_ dateStr:String, dateFormat:String = "yyyy-MM-dd HH:mm:ss", locale:String = "zh_CN", repeatInterval:RepeatInterval = .NotRepeat, repeats:Bool = false)
某段时间触发,返回
UNCalendarNotificationTrigger
,getAllDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> ())
获取所有保留在通知中心的通知
removeDeliveredNotifications(_ identifiers: [String])
根据
identifier
保留在通知中心的通知removeAllDeliveredNotifications()
移除所有保留在通知中心的通知
getAllPendingNotificationRequests(completionHandler: @escaping ([UNNotificationRequest])->())
获取所有尚未触发的通知
removeAllPendingNotificationRequests()
移除所有尚未触发的通知
removePendingNotificationRequests(_ identifiers: [String])
移除指定
identifier
尚未触发的通知getApplicationIconBadge()
获取app角标数
setApplicationIconBadge(_ number:Int)
设置app角标数
enum: RepeatInterval
@objc public enum RepeatInterval : Int {
case NotRepeat
case Day
case Week
case Month
case Year
}
使用
// 10秒后触发
LocalNotification.sendLocalNotification(withIdentifier: "timeInterIdentifier", body: "需要发送的正文", tigger: LocalNotification.createTimeIntervalTrigger(timeInterval: 10))
// "2017-1-1 01:01:01"触发
LocalNotification.sendLocalNotification(withIdentifier: "calendarInterIdentifier", title: "标题", subtitle: "子标题", body: "正文", bage: 1, tigger: LocalNotification.createCalendarTrigger("2017-1-1 01:01:01"), attachments: [imageAttachment], sound: nil)
-- AppDelegate.swift
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// 监听UNUserNotificationCenter代理
let center = UNUserNotificationCenter.current()
center.delegate = self
return true
}
// 如要app在前台也能接收到通知,设置此方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// do something
// 回调,设置前台也能收到本地通知
completionHandler([.alert,.badge,.sound])
}
// 点击通知触发此方法
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// do something
// example
LocalNotification.setApplicationIconBadge(0)
// 回调
completionHandler()
}