import UIKit
import UserNotifications
typealias NotificationManagerAddResultCallBack = ((Error?) -> Void)?
typealias NotificationManagerGetResultCallBack = (([UNNotificationRequest]?) -> Void)?
extension GHFNotificationManager {
class func addLocationNoti(identifier: String?, title: String?, userInfo: [String: Any]?, timeInterval: TimeInterval, completionHandler: NotificationManagerAddResultCallBack) {
addLocationNoti(identifier: identifier, title: title, subTitle: nil, userInfo: userInfo, timeInterval: timeInterval, completionHandler: completionHandler)
}
class func addLocationNoti(identifier: String?, title: String?, subTitle: String?, userInfo: [String: Any]?, timeInterval: TimeInterval, completionHandler: NotificationManagerAddResultCallBack) {
let content = UNMutableNotificationContent.init()
content.title = title ?? ""
content.subtitle = subTitle ?? ""
content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "noticeMusic.mp3"))
content.userInfo = userInfo ?? Dictionary()
#if DEBUG
GHFToast.toast("\(timeInterval)s后提醒")
#endif
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: false)
addLocationNoti(identifier: identifier, content: content, trigger: trigger, completionHandler: completionHandler)
}
class func addLocationNoti(identifier: String?, content: UNMutableNotificationContent, trigger: UNNotificationTrigger, completionHandler: NotificationManagerAddResultCallBack) {
if let identifier = identifier {
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
print("\n----------------------------添加提醒回调START----------------------------\n error:\(String(describing: error))\n identifier:\(identifier)\n title:\(content.title)\n userInfo:\(content.userInfo)\n timeInterval:\((trigger as! UNTimeIntervalNotificationTrigger).timeInterval)\n ----------------------------添加提醒回调END----------------------------\n")
completionHandler?(error)
}
}
}
}
extension GHFNotificationManager {
class func removeLocalNoti(noticIds: [String]?) {
if let noticIds = noticIds {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: noticIds)
}
}
class func removeAllLocalNotification() {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
}
extension GHFNotificationManager {
class func getAllLocationNotification(completionHandler: NotificationManagerGetResultCallBack) {
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
completionHandler?(requests)
}
}
}