如何对 iPhone 中的 whatsapp 或 iMessage 等通知进行快速回复?

Posted

技术标签:

【中文标题】如何对 iPhone 中的 whatsapp 或 iMessage 等通知进行快速回复?【英文标题】:How to do Quick Replay of notification like whats app or iMessage in iPhone? 【发布时间】:2017-11-24 09:52:30 【问题描述】:

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

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

【问题讨论】:

iphonelife.com/content/how-to-respond-to-notifications-ios-10 【参考方案1】:

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

【讨论】:

以上是关于如何对 iPhone 中的 whatsapp 或 iMessage 等通知进行快速回复?的主要内容,如果未能解决你的问题,请参考以下文章

whatsapp如何在第一次在iphone上运行时知道国家代码

whatsapp html链接在iphone 8,chrome上不起作用

Whatsapp 链接

如何对像whatsapp这样的android通知进行分组?

如何玩转WhatsApp,WhatsApp连不上网络解决方法

如何使用离子3中的特定联系人从其他应用程序打开Whatsapp应用程序?