swift 3 中的本地通知新 UI

Local notification in swift 3 with new UI

我知道如何在 Swift 3 中创建本地通知(我是这部分的新手),但是,我想创建如下图所示的内容。所有tutorials in the web都太老了,我不知道该怎么办。

正如您在扩展通知之前看到的,有 2 个按钮。扩展后还有 2 个红色和蓝色按钮。

已更新

谢谢乔恩

滑动手势只显示清晰。是否有显示 clearview

的任何设置

红色和蓝色按钮仅在 iOS 10 之前的 iOS 版本中可用。在 iOS 10 中,通知设计发生了变化。滑动手势用于标准动作 ClearView.自定义操作 SnoozeConfirm 将在以下时间显示您用力触摸通知或将其拉下(对于没有用力触摸的设备)。如果您使用的是强制触摸设备,则可能不会显示 View 按钮。

按钮现在看起来不一样了:

因此,以下是使用 Swift 3 / 4 实现本地通知的方法:

对于 iOS 10 之前的 iOS 版本:

如果您支持 iOS 之前的 iOS 版本,则必须使用旧版本(已弃用 iOS 10)UILocalNotification:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        registerLocalNotification()
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        scheduleLocalNotification()
    }

    func scheduleLocalNotification() {
        let localNotification = UILocalNotification()
        localNotification.alertTitle = "Buy milk"
        localNotification.alertBody = "Remember to buy milk from store"
        localNotification.fireDate = Date(timeIntervalSinceNow: 3)
        localNotification.soundName = UILocalNotificationDefaultSoundName
        localNotification.category = "reminderCategory" // Category to use the specified actions
        UIApplication.shared.scheduleLocalNotification(localNotification) // Scheduling the notification.
    }

    func registerLocalNotification() {
        let reminderActionConfirm = UIMutableUserNotificationAction()
        reminderActionConfirm.identifier = "Confirm"
        reminderActionConfirm.title = "Confirm"
        reminderActionConfirm.activationMode = .background
        reminderActionConfirm.isDestructive = false
        reminderActionConfirm.isAuthenticationRequired = false

        let reminderActionSnooze = UIMutableUserNotificationAction()
        reminderActionSnooze.identifier = "Snooze"
        reminderActionSnooze.title = "Snooze"
        reminderActionSnooze.activationMode = .background
        reminderActionSnooze.isDestructive = true
        reminderActionSnooze.isAuthenticationRequired = false

        // Create a category with the above actions
        let shoppingListReminderCategory = UIMutableUserNotificationCategory()
        shoppingListReminderCategory.identifier = "reminderCategory"
        shoppingListReminderCategory.setActions([reminderActionConfirm, reminderActionSnooze], for: .default)
        shoppingListReminderCategory.setActions([reminderActionConfirm, reminderActionSnooze], for: .minimal)

        // Register for notification: This will prompt for the user's consent to receive notifications from this app.
        let notificationSettings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: [shoppingListReminderCategory])

        UIApplication.shared.registerUserNotificationSettings(notificationSettings)
    }
}

这将注册本地通知并在用户关闭应用程序 3 秒后触发它(用于测试目的)

对于 iOS 10 及更高版本:

如果您将应用定位到 iOS 10,则可以使用新的 UserNotifications 框架:

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        registerUserNotifications()
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        scheduleLocalNotification()
    }

    func registerUserNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
            guard granted else { return }
            self.setNotificationCategories()
        }
    }

    func setNotificationCategories() {
        // Create the custom actions
        let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION",
                                                title: "Snooze",
                                                options: .destructive)
        let confirmAction = UNNotificationAction(identifier: "CONFIRM_ACTION",
                                              title: "Confirm",
                                              options: [])

        let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED",
                                                     actions: [snoozeAction, confirmAction],
                                                     intentIdentifiers: [],
                                                     options: UNNotificationCategoryOptions(rawValue: 0))

        // Register the category.
        let center = UNUserNotificationCenter.current()
        center.setNotificationCategories([expiredCategory])
    }

    func scheduleLocalNotification() {
        let content = UNMutableNotificationContent()
        content.title = "Buy milk!"
        content.body = "Remember to buy milk from store!"
        content.categoryIdentifier = "TIMER_EXPIRED"

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)

        // Create the request object.
        let request = UNNotificationRequest(identifier: "Milk reminder", content: content, trigger: trigger)

        // Schedule the request.
        let center = UNUserNotificationCenter.current()
        center.add(request) { (error : Error?) in
            if let theError = error {
                print(theError.localizedDescription)
            }
        }
    }
}

您可以查看使用 UserNotifications 框架的演示应用 here