ios - 在 iOS 10 中实现 "reply to notification from lock screen"

标签 ios

我们有一个消息传递应用程序,旨在在手机锁定时从远程用户收到消息时显示通知,并让本地用户从锁定屏幕输入文本并发送消息。我该如何实现? iOS 10 中的 UNUserNotificationCenter 是正确的选择吗?

谢谢。

最佳答案

互联网上缺乏结构良好的信息,尽管它是非常好的功能,在严肃的 Messenger 应用程序中实现。

您应该从 UNNotificationContentExtension 开始,以显示接收到的推送通知的自定义 UI。以互联网上的任何可用示例为例,并按照您的意愿实现它。注意 bundle ID - 它应该是 com.yourapp.yourextension。完成后,您将在 Xcode 中拥有主应用程序和扩展小部件。

在主应用中设置以 iOS10 方式注册推送通知:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
        (granted, error) in
        guard granted else { return }
        let replyAction = UNTextInputNotificationAction(identifier: "ReplyAction", title: "Reply", options: [])
        let openAppAction = UNNotificationAction(identifier: "OpenAppAction", title: "Open app", options: [.foreground])
        let quickReplyCategory = UNNotificationCategory(identifier: "QuickReply", actions: [replyAction, openAppAction], intentIdentifiers: [], options: [])
        UNUserNotificationCenter.current().setNotificationCategories([quickReplyCategory])
        
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            guard settings.authorizationStatus == .authorized else { return }
            UIApplication.shared.registerForRemoteNotifications()
        }
    }

所有魔法都发生在您添加到推送通知处理程序的 UNTextInputNotificationAction 自定义操作中。

要完成推送通知设置,请​​在您的扩展 Info.plist 中添加此参数:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory: "QuickReply"

这一切都与设置有关。要尝试它,请使用 Pusher 工具并以这种方式配置推送通知:

{
    "aps": {
        "alert":"Trigger quick reply",
        "category":"QuickReply"
    }
}

至少你必须在你的小部件中捕捉到通知。它发生在您的小部件类的 func didReceive(_ notification: UNNotification) 中:

func didReceive(_ notification: UNNotification) {
    let message = notification.request.content.body
    let userInfo = notification.request.content.userInfo
    // populate data from received Push Notification in your widget UI...
}

如果用户响应收到的推送通知,您的小部件将触发以下回调:

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
    if response.actionIdentifier == "ReplyAction" {
        if let textResponse = response as? UNTextInputNotificationResponse {
            // Do whatever you like with user text response...
            completion(.doNotDismiss)
            return
        }
    }
    completion(.dismiss)
}

关于ios - 在 iOS 10 中实现 "reply to notification from lock screen",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583736/

相关文章:

ios - 如何使用项目的价格对 UICollectionView 项目进行排序/排序?

iphone - ios:通过 iPhone/iPad 代码打开/关闭飞行模式

ios - 为什么调用我的 CABasicAnimation 在 vi​​ewDidAppear 中不起作用?

ios - 为什么我的 SearchController 只匹配第一个字符?

ios - ios 8 中的 voip 应用程序是否有新要求?

ios - 将 WCSession 与多个 ViewController 一起使用

ios - ios9.3上xcode 7.3中的路径似乎没有有效的已编译 Storyboard

iphone - 将对象从NSMutableDictionary添加到NSMutableArray

ios - Xcode 11 GM Seed 工具链无效

iphone - 从 iPhone 应用程序到通用应用程序,用户界面注意事项