4

系統將加載通知服務擴展並在iOS 10中調用其didReceive(_:withContentHandler:)進行本地通知? 如果是的話我們該怎麼做?本地通知的通知服務擴展

+0

http://www.appcoda.com/ios10-user-notifications-guide/ –

+0

這裏接受的答案似乎是錯誤的。 –

回答

0

您需要創建一個Notification Content Extension來顯示iOS10的自定義通知。在Xcode菜單欄中,轉到文件 - >新建 - >目標。然後從列表中選擇Notification Content Extension。 enter image description here

輸入相應的詳細信息並單擊芬蘭語。您將看到一個新文件夾,其中包含您的擴展名。在該文件夾中,將會有3個文件:

  1. NotificationViewController:在這裏,您可以設計自定義界面並實現響應。

  2. MainStoryboard:您可以使用它來設計您的自定義通知。

  3. 的Info.plist

在Info.plist文件,添加以下內容: enter image description here

這將是調度通知時,你會在你的主要項目中使用的類標識符。

let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [], intentIdentifiers:[], options: []) 
      UNUserNotificationCenter.current().setNotificationCategories([category]) 
      content.categoryIdentifier = "myNotificationCategory" 

您的NotificationViewController類應該看起來像這樣。

func didReceive(_ notification: UNNotification) { 
     //change properties of notification here. 
    } 

    func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) { 
     //implement response logic here. 
    } 

有幾個很好的在線教程。你可以檢查herehere & here。 希望這有助於。

+2

我還想說清楚問題是您是否可以使用通知服務擴展來進行本地通知。嗯,是的,你可以。 – Morgz

+0

@Morgz我認爲我們不能使用通知服務擴展來進行本地通知。 – nuynait

3

不可以。接受的答案描述通知內容擴展,它允許您在擴展的通知視圖中顯示ViewController,並使用遠程和本地通知。

通知服務擴展,可以讓你改變通知的內容(以圖片等)都與本地通知工作。但是,您可以附加圖像作爲過程的一部分來顯示本地通知。

+0

您是否偶然有文檔或WWDC通話鏈接來確認這是不可能的? –