ios - 如何在 iPhone 中快速重播通知,如 whats app 或 iMessage?

标签 ios objective-c iphone push-notification chat

我想在 iPhone 应用程序中为 Quick replay 编写代码,请问我可以实现吗?我已经尝试过通知扩展,但无法重播。

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

enter image description here

最佳答案

您可以使用 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()
    }
}

iOS Quick Reply in notification

关于ios - 如何在 iPhone 中快速重播通知,如 whats app 或 iMessage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470579/

相关文章:

iphone - 作为 subview 访问时如何为uibutton设置图片

iphone - 从 iDevice 上传视频到 FTP

iphone - 围绕屏幕外的一个点旋转 UIImageView? (uiImageView 的中心

iphone - 更改位于主 ViewController 类的单独 .xib 中的标签文本

ios - 如何正确生成时间戳

ios - 如何在 VB.Net 中发送和配置 APN 授权 key (.p8 文件)

iphone - 我的 UIImageView 中的大图像导致我的 UINavigationController 堆栈中的较低 View 释放......我该如何解决这个问题?

ios - 自定义单元格按钮单击

objective-c - 如何在 Xcode 中为 iPhone SDK 构建自定义控件?

ios - 为什么不允许我的团队管理 iCloud 容器?