【问题标题】:Implementing "reply to notification from lock screen" in iOS 10在 iOS 10 中实现“从锁屏回复通知”
【发布时间】:2016-09-19 23:56:08
【问题描述】:

我们有一个消息应用程序,旨在在手机被远程用户锁定时收到消息时显示通知,并让本地用户从锁定屏幕输入文本并发送消息。我该如何实施? iOS 10 中的 UNUserNotificationCenter 是否可行?

谢谢。

【问题讨论】:

    标签: ios


    【解决方案1】:

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

    您应该以UNNotificationContentExtension 开头,以显示接收到的推送通知的自定义 UI。以互联网上的任何可用示例并按照您的意愿实施。注意捆绑 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)
    }
    

    【讨论】:

    • 我已按照您的步骤...而且效果很好..但我被困在“ReplyAction”中。我想在ReplyAction 调用API。是否可以在通知内容扩展中调用 API?如果可以,请指导我如何实现这一目标?
    • @bittu 当然,您可以在那里运行任何代码。调用 API 请求时,这意味着您有一些完成处理程序,您可以在其中从 API 获得响应。在该处理程序中,您调用yourNotificationContentExtension.completionHandler(.dismiss)。另一个问题是如何将 API 凭据从主应用程序传递到扩展程序 - 您应该为此使用共享的 User Defaults
    • @bittu 我也在尝试在操作按钮上调用 API,但我在 alamofire 中遇到网络丢失错误...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 2017-02-12
    • 1970-01-01
    相关资源
    最近更新 更多