//检查是否已经授权本地通知 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { //本地通知授权 application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)) }
//本地通知设置 let content = UNMutableNotificationContent() content.title = "Title" content.body = "This is a test notification" content.sound = UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "testNotification", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
//应用程序的委托方法 func application(_ application: UIApplication, didReceive notification: UILocalNotification) { //通知处理 application.applicationIconBadgeNumber = 0 }
//通知中心的委托方法 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .badge, .sound]) }