6.4 系统 本地通知

本地通知(可做闹钟功能): 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
   //    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容