Swift Local push notification action view

匆匆过客 提交于 2020-06-25 06:30:26

问题


I'm trying to create a notification with two possible action buttons like "Quick reply" and "Cancel", but I can't find any code examples for this. Please could someone explain how to do this in Swift 3.


回答1:


It's difficult to give a precise example of what you are looking for. Here's a quick UNUserNotificationCenter implementation with an action.

import UserNotifications

Define a category ID constant

private let categoryID = "Category"

Setup and register 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])
}

Example event for triggering the notification

// 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 implementation

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

Handle user notification authorization

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 implementation

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

Result




回答2:


You can achieve this by UIMutableUserNotificationAction

for more information look here:-

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

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



来源:https://stackoverflow.com/questions/43999569/swift-local-push-notification-action-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!