2016-09-19 65 views
3

我們有一個通訊應用程序,旨在當手機被遠程用戶鎖定時接收消息時顯示通知,並讓本地用戶從鎖定屏幕輸入文本,併發送信息。我如何實現這一點? UNUserNotificationCenter在iOS 10中是否可行?在iOS 10中執行「鎖定屏幕通知回覆」

謝謝。

回答

3

在互聯網上缺乏結構良好的信息,雖然它是非常好的功能,在嚴肅的信使應用程序中實現。

您應該從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) 
}