6.4 系統(tǒng) 本地通知

本地通知(可做鬧鐘功能): import UserNotifications UNUserNotificationCenterDelegate

    //--- 本地通知: import UserNotifications UNUserNotificationCenterDelegate
   @available(iOS 10.0, *) //用于函數(shù)、類、協(xié)議,限制平臺或系統(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 //程序圖標右上角 顯示通知的數(shù)量;
       
       ///
       let eDicInfo = NSDictionary(object: "數(shù)據(jù)信息", forKey: "eKeyN" as NSCopying) //用來在通知中傳遞數(shù)據(jù);
       eNotifi.userInfo = eDicInfo as [NSObject : AnyObject] //設(shè)置通知的信息;
       
       /// 設(shè)置 發(fā)送通知的時機: 15s后發(fā)送,  不重復(fù)發(fā)送 repeats: false
       let eTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 15.0, repeats: false)
       
       /// 設(shè)置通知的信息: identifier 標識符,
       let eNotiRequest = UNNotificationRequest.init(identifier: "TyeNotifi", content: eNotifi, trigger: eTrigger)
       
       /// 把通知請求加到用戶通知中心, 等待定時發(fā)送:
       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中——程序?qū)⒁顺鰰r:
   //    func sceneWillResignActive(_ scene: UIScene) {
   //        //--- 將程序圖標右上角的數(shù)字-1
   //        UIApplication.shared.applicationIconBadgeNumber -= 1
   //    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容