在iOS 10中执行“锁定屏幕通知回复”

问题描述:

我们有一个通讯应用程序,旨在当手机被远程用户锁定时接收消息时显示通知,并让本地用户从锁定屏幕输入文本,并发送信息。我如何实现这一点? UNUserNotificationCenter在iOS 10中是否可行?在iOS 10中执行“锁定屏幕通知回复”

谢谢。

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

您应该从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: "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自定义操作。

要完成你的扩展Info.plist设置推送通知添加此PARAM:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory: "QuickReply"

这是所有关于建立。尝试一下,使用Pusher工具,以这种方式配置推送通知:

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

至少你必须抓住通知在你的widget。它发生在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... 
} 

如果用户收到推送通知的响应,你的widget将触发​​以下的回调:

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