Swift 本地推送通知操作 View

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

我正在尝试创建一个带有两个可能的操作按钮(例如“快速回复”和“取消”)的通知,但我找不到任何相关的代码示例。请有人解释一下如何在 Swift 3 中执行此操作。

最佳答案

很难给出您正在寻找的内容的精确示例。下面是一个带有操作的快速 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

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

相关文章:

ios - 如何在 iOS 中使用 Contacts Framework 获取更改的联系人

android - 是否可以在通过电话发送的智能 watch 上发出声音通知?

swift - 需要澄清 Swift 类型属性

ios - 又名 'NSString' 没有成员 - Swift

ios - 快速读取远程通知的userInfo

xcode - 通过解析将推送通知发送到您的 iPhone 作为测试

java - 创建具有不同 Intent 的多个通知

ios - 如何在 Realm Swift 中将子类添加到 List<superclass>

iphone - 1 个用于多个应用程序的推送通知 SSL 证书

android - 在自定义 BroadcastReceiver 中未在 Android 上接收解析推送通知