实施“答复来自锁定屏幕的通知".在iOS 10中 [英] Implementing "reply to notification from lock screen" in iOS 10

查看:89
本文介绍了实施“答复来自锁定屏幕的通知".在iOS 10中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a messaging app that aims to display notifications when it receives messages when phone is locked from remote user, and let local user enter text from lock screen, and send the message. How do I implement this? Is UNUserNotificationCenter in iOS 10 the way to go?

谢谢.

推荐答案

Internet缺乏结构良好的信息,尽管它是非常好的功能,已在严肃的Messenger应用程序中实现.

There is lack of well-structured info in internet, although it's very good feature, implemented in serious messenger apps.

您应该以UNNotificationContentExtension开头以显示接收到的推送通知的自定义UI.以互联网上的任何可用示例为例,并根据需要实施它.请注意捆绑软件ID-应该为com.yourapp.yourextension.完成后,您将在Xcode中拥有主应用程序和扩展小部件.

You should start with UNNotificationContentExtension to show custom UI for received Push Notification. Take any available example on internet and implement it like you want. Pay attention on bundle ID - it should be com.yourapp.yourextension. When done, you'll have the main app and the extension widget in Xcode.

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

In the main app set up registration for Push Notifications in iOS10 manner:

    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: "QuickChat", actions: [replyAction, openAppAction], intentIdentifiers: [], options: [])
        UNUserNotificationCenter.current().setNotificationCategories([quickReplyCategory])

        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            guard settings.authorizationStatus == .authorized else { return }
            UIApplication.shared.registerForRemoteNotifications()
        }
    }

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

All magic happens in UNTextInputNotificationAction custom action that you add to your Push Notifications handler.

要完成设置推送通知,请在您的扩展名 Info.plist:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory: "QuickReply"

To complete setting up Push Notifications add this param in your extension Info.plist: NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory: "QuickReply"

一切都与设置有关.若要尝试,请使用Pusher工具并按以下方式配置推送通知":

It's all about setting up. To try it, use Pusher tool and configure Push Notification in this manner:

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

至少您必须在小部件中捕获通知.它发生在您的窗口小部件类的func didReceive(_ notification: UNNotification)中:

At least you have to catch the notification in your widget. It happens in func didReceive(_ notification: UNNotification) in your widget class:

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...
}

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

And if user responds to received Push Notification, your widget will trigger the following callback:

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 10中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆