我正试图用两个可能的操作按钮(如“快速回复”和“取消”)创建一个通知,但找不到任何代码示例。请有人在swift 3中解释一下怎么做。

最佳答案

很难给出一个准确的例子来说明你在寻找什么。下面是一个快速的、带有操作的实现。

import UserNotifications

定义类别ID常量
private let categoryID = "Category"

设置并注册UNUserNotificationCenter
// MARK: - Lifecycle

override func viewDidLoad() {
    super.viewDidLoad()
    // Configure User Notification Center
    UNUserNotificationCenter.current().delegate = self
    // Define Actions
    let actionShowSomething = UNNotificationAction(identifier: "ShowSomething", title: "Show Something", options: [])

    // Define Category
    let category = UNNotificationCategory(identifier: categoryID, actions: [actionShowSomething], intentIdentifiers: [], options: [])

    // Register Category
    UNUserNotificationCenter.current().setNotificationCategories([category])
}

触发通知的示例事件
// MARK: - Actions

@IBAction func sheduleNotification(sender: UIButton) {
    // Request Notification Settings
    UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
        switch notificationSettings.authorizationStatus {
        case .notDetermined:
            self.requestAuthorization(completionHandler: { (success) in
                guard success else { return }

                // Schedule Local Notification
                self.scheduleLocalNotification()
            })
        case .authorized:
            // Schedule Local Notification
            self.scheduleLocalNotification()
        case .denied:
            print("Application Not Allowed to Display Notifications")
        }
    }
}

UNUserNotificationCenter实施
// MARK: - Methods

private func scheduleLocalNotification() {
    // Create Notification Content
    let notificationContent = UNMutableNotificationContent()

    // Configure Notification Content
    notificationContent.title = "Title"
    notificationContent.subtitle = "Subtitle"
    notificationContent.body = "Body"

    // Set Category Identifier
    notificationContent.categoryIdentifier = categoryID

    // Add Trigger
    let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

    // Create Notification Request
    let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

    // Add Request to User Notification Center
    UNUserNotificationCenter.current().add(notificationRequest) { (error) in
        if let error = error {
            print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
        }
    }
}

处理用户通知授权
private func requestAuthorization(completionHandler: @escaping (_ success: Bool) -> ()) {
    // Request Authorization
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in
        if let error = error {
            print("Request Authorization Failed (\(error), \(error.localizedDescription))")
        }
        completionHandler(success)
    }
}

sheduleNotification实施
extension ViewController: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse, withCompletionHandler
    completionHandler: @escaping () -> Void) {
        completionHandler()
    }
}

结果
swift - Swift Local推送通知操作 View-LMLPHP

关于swift - Swift Local推送通知操作 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43999569/

10-11 22:26
查看更多