Ios 如何在iPhone中快速重播通知,如whats应用程序或iMessage?

Ios 如何在iPhone中快速重播通知,如whats应用程序或iMessage?,ios,objective-c,iphone,push-notification,chat,Ios,Objective C,Iphone,Push Notification,Chat,我想在iPhone应用程序中编写快速回放的代码,请知道我们可以实现吗?我尝试了通知扩展,但无法使用它进行重播 如下面的快照所示,我想在不打开应用程序的情况下进行相同的聊天重播。当应用程序收到消息时 您可以使用UNTextInputNotificationAction并处理使用UNTextInputNotificationResponse输入的文本 下面是一个工作示例 import UIKit class ViewController: UIViewController {

我想在iPhone应用程序中编写快速回放的代码,请知道我们可以实现吗?我尝试了通知扩展,但无法使用它进行重播

如下面的快照所示,我想在不打开应用程序的情况下进行相同的聊天重播。当应用程序收到消息时


您可以使用
UNTextInputNotificationAction
并处理使用
UNTextInputNotificationResponse
输入的文本

下面是一个工作示例


import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var userTextLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Part 1: Setup
        let textAction = UNTextInputNotificationAction(identifier: "TextAction",
                                                       title: "",
                                                       options: .authenticationRequired,
                                                       textInputButtonTitle: "Send",
                                                       textInputPlaceholder: "Type here")
        
        let quickReplyCategory = UNNotificationCategory(identifier: "QUICK_REPLAY",
                                                        actions: [textAction],
                                                        intentIdentifiers: [],
                                                        options: .customDismissAction)
        
        let center = UNUserNotificationCenter.current()
        // Request permission to display alerts and play sounds.
        center.requestAuthorization(options: [.alert, .sound])
        { (granted, error) in
            // Enable or disable features based on authorization.
        }
        
        center.setNotificationCategories([quickReplyCategory])
    }
    
    @IBAction func buttonPress(_ sender: UIButton) {
        
        // Part 2: Trigger
        let now = Date()
        
        var components = Calendar.current.dateComponents([.hour, .minute, .second], from: now)
        components.second = (components.second ?? 0) + 2
        
        let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
        
        // Content
        let content = UNMutableNotificationContent()
        content.title = "New message"
        content.body = "Sounds good?"
        
        content.categoryIdentifier = "QUICK_REPLAY"
        
        // Create the request
        let uuidString = UUID().uuidString
        let request = UNNotificationRequest(identifier: uuidString,
                                            content: content, trigger: trigger)
        
        // Schedule the request with the system.
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.add(request) { (error) in
            if error != nil {
                // Handle any errors.
            }
        }
        
        notificationCenter.delegate = self
    }
    
}


// PART 3: Handle
extension ViewController: UNUserNotificationCenterDelegate {
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .badge, .sound])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler:
        @escaping () -> Void) {
        
        // Perform the task associated with the action.
        print(response.actionIdentifier)
        
        if let textResponse = (response as? UNTextInputNotificationResponse) {
            userTextLabel.text = textResponse.userText
        }
        
        // Always call the completion handler when done.
        completionHandler()
    }
}