gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:39:40 27 4
gpt4 key购买 nike

我们有一个消息传递应用程序,旨在在手机锁定时从远程用户收到消息时显示通知,并让本地用户从锁定屏幕输入文本并发送消息。我该如何实现? 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com