Swift3 swift 3中带有新UI的本地通知

Swift3 swift 3中带有新UI的本地通知,swift3,uilocalnotification,Swift3,Uilocalnotification,我知道如何在Swift 3中创建本地通知(这部分我是新手),但是,我想创建类似下图的内容。都太老了,我不知道该怎么办 在扩展通知之前可以看到,有两个按钮。扩展后还有两个红色和蓝色按钮 已更新 谢谢你,乔恩 幻灯片手势仅显示清除。是否有显示清除和查看的设置 红色和蓝色按钮仅在iOS 10之前的iOS版本中可用。在iOS 10中,通知设计发生了变化。滑动手势用于标准动作清除和查看。当您强制触摸通知或将其下拉(对于没有强制触摸的设备)时,将显示自定义操作Snooze和Confirm。如果您使用的是强

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

在扩展通知之前可以看到,有两个按钮。扩展后还有两个红色和蓝色按钮

已更新

谢谢你,乔恩

幻灯片手势仅显示清除。是否有显示清除查看的设置


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

按钮现在看起来不同了:

因此,以下是如何使用Swift 3/4实现本地通知:

对于iOS 10之前的iOS版本:

如果您支持iOS10之前的iOS版本,则必须使用旧版本(不推荐使用iOS10)
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框架的演示应用程序

Merci,非常清楚!但是,幻灯片手势仅显示清晰,请参见我的更新问题。是否有显示clear和View的设置?您是否在具有强制触摸功能的设备上运行应用程序?在这种情况下,有时不显示“查看”按钮。我在github中添加了一个使用UserNotifications框架的演示应用程序:force touch等于3D touch?我在iphone 7 plus上运行(在模拟器上)。非常感谢github项目。是的,force touch和3D touch是一样的。