Swift 本地推送通知操作视图

Posted

技术标签:

【中文标题】Swift 本地推送通知操作视图【英文标题】:Swift Local push notification action view 【发布时间】:2017-05-16 10:58:03 【问题描述】:

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

【问题讨论】:

您是否尝试使用 ios 10? 是的@SalmanGhumsani 【参考方案1】:

很难给出一个准确的例子来说明您正在寻找什么。这是一个带有操作的快速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()
    

结果

【讨论】:

【参考方案2】:

您可以通过UIMutableUserNotificationAction实现此目的

更多信息请看这里:-

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

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

【讨论】:

以上是关于Swift 本地推送通知操作视图的主要内容,如果未能解决你的问题,请参考以下文章

Swift 本地推送通知UILocalNotification

当用户使用 iOS 13 Swift 5 点击推送通知时,在特定视图中打开应用程序

通过 Swift 关闭推送通知功能

带有 Rpush gem 的 IOS/SWIFT 推送通知:生产中的无效令牌 (8)

在推送通知 Swift 2 AppDelegate 上加载特定的 viewController

如何处理未打开的推送通知(iOS、Swift)