本地通知(可做鬧鐘功能): import UserNotifications UNUserNotificationCenterDelegate
//--- 本地通知: import UserNotifications UNUserNotificationCenterDelegate
@available(iOS 10.0, *) //用于函數(shù)、類(lèi)缺猛、協(xié)議,限制平臺(tái)或系統(tǒng)版本;
func yeUserNotifications(){
let eNotifi = UNMutableNotificationContent()
eNotifi.sound = UNNotificationSound.default //提示音;
eNotifi.body = NSString.localizedUserNotificationString(forKey: "這是本地通知的內(nèi)容", arguments: nil)
eNotifi.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber //程序圖標(biāo)右上角 顯示通知的數(shù)量;
///
let eDicInfo = NSDictionary(object: "數(shù)據(jù)信息", forKey: "eKeyN" as NSCopying) //用來(lái)在通知中傳遞數(shù)據(jù);
eNotifi.userInfo = eDicInfo as [NSObject : AnyObject] //設(shè)置通知的信息;
/// 設(shè)置 發(fā)送通知的時(shí)機(jī): 15s后發(fā)送, 不重復(fù)發(fā)送 repeats: false
let eTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 15.0, repeats: false)
/// 設(shè)置通知的信息: identifier 標(biāo)識(shí)符辆布,
let eNotiRequest = UNNotificationRequest.init(identifier: "TyeNotifi", content: eNotifi, trigger: eTrigger)
/// 把通知請(qǐng)求加到用戶通知中心, 等待定時(shí)發(fā)送:
let eNotiCenter = UNUserNotificationCenter.current()
eNotiCenter.add(eNotiRequest)
/// 再到 AppDelegate 和 SceneDelegate 中....
}
/// AppDelegate中——程序啟動(dòng)時(shí): 注冊(cè)通知:
// func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//
// //--- 本地通知:
// /// 獲得用戶通知的單例對(duì)象:
// let eNoti = UNUserNotificationCenter.current()
//
// /// 注冊(cè)通知:
// eNoti.requestAuthorization(options: [.alert, .badge, .sound]) { (eGranted:Bool, eE:Error?) in
// }
// return true
// }
/// SceneDelegate中——程序?qū)⒁顺鰰r(shí):
// func sceneWillResignActive(_ scene: UIScene) {
// //--- 將程序圖標(biāo)右上角的數(shù)字-1
// UIApplication.shared.applicationIconBadgeNumber -= 1
// }