本地通知(可做闹钟功能): import UserNotifications UNUserNotificationCenterDelegate
//--- 本地通知: import UserNotifications UNUserNotificationCenterDelegate
@available(iOS 10.0, *) //用于函数、类、协议,限制平台或系统版本;
func yeUserNotifications(){
let eNotifi = UNMutableNotificationContent()
eNotifi.sound = UNNotificationSound.default //提示音;
eNotifi.body = NSString.localizedUserNotificationString(forKey: "这是本地通知的内容", arguments: nil)
eNotifi.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber //程序图标右上角 显示通知的数量;
///
let eDicInfo = NSDictionary(object: "数据信息", forKey: "eKeyN" as NSCopying) //用来在通知中传递数据;
eNotifi.userInfo = eDicInfo as [NSObject : AnyObject] //设置通知的信息;
/// 设置 发送通知的时机: 15s后发送, 不重复发送 repeats: false
let eTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 15.0, repeats: false)
/// 设置通知的信息: identifier 标识符,
let eNotiRequest = UNNotificationRequest.init(identifier: "TyeNotifi", content: eNotifi, trigger: eTrigger)
/// 把通知请求加到用户通知中心, 等待定时发送:
let eNotiCenter = UNUserNotificationCenter.current()
eNotiCenter.add(eNotiRequest)
/// 再到 AppDelegate 和 SceneDelegate 中....
}
/// AppDelegate中——程序启动时: 注册通知:
// func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//
// //--- 本地通知:
// /// 获得用户通知的单例对象:
// let eNoti = UNUserNotificationCenter.current()
//
// /// 注册通知:
// eNoti.requestAuthorization(options: [.alert, .badge, .sound]) { (eGranted:Bool, eE:Error?) in
// }
// return true
// }
/// SceneDelegate中——程序将要退出时:
// func sceneWillResignActive(_ scene: UIScene) {
// //--- 将程序图标右上角的数字-1
// UIApplication.shared.applicationIconBadgeNumber -= 1
// }