Swift Local推送通知操作视图

时间:2017-05-16 10:58:04

标签: swift push-notification notifications apple-push-notifications uilocalnotification

我尝试使用两个可能的操作按钮创建通知,例如"快速回复"和"取消",但我找不到任何代码示例。请有人在Swift 3中解释如何做到这一点。

2 个答案:

答案 0 :(得分:2)

很难给出您正在寻找的确切示例。这是一个快速UNUserNotificationCenter实施的动作。

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")
        }
    }
}

sheduleNotification 实施

// 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)
    }
}

UnUserNotificationDelegate 实施

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()
    }
}

<强>结果

Push example at runtime

答案 1 :(得分:0)

您可以通过AttributeError

来实现这一目标

了解更多信息,请点击此处: -

1)https://nrj.io/simple-interactive-notifications-in-ios-8/

2)http://www.imore.com/interactive-notifications-ios-8-explained