Notification service extension for Local notificat

2019-02-17 10:20发布

问题:

will system loads Notification service extension and calls its didReceive(_:withContentHandler:) for local notifications in iOS 10? If yes how we can do that?

回答1:

No. The accepted answer describes Notification Content Extensions, which allow you to present a ViewController in the expanded notification view, and works with both remote and local notification.

Notification Service Extensions, that let you change the content of the notification (attaching images, etc) do not work with local notifications. However, you can attach images as part of the process to show a local notification.



回答2:

You need to create a Notification Content Extension for displaying custom notification with iOS10. In the Xcode menu bar, go to File->New->Target. Then from the list select Notification Content Extension.

Enter the corresponding details and click Finnish. You will see a new folder with the name of your extension. In the folder, there will be 3 files :

  1. NotificationViewController : Here you can design your custom interface and implement responses.

  2. MainStoryboard : You can use this to design your custom notification.

  3. Info.plist

In the Info.plist file, add the following:

This will be the category identifier you will use in your main project when scheduling notifications.

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

Your NotificationViewController class should look something like this.

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

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

There are a couple of good tutorials available online. You can check here, here & here. Hope this helps.



回答3:

Notification Service extension is for remote notification not for local notification.

As per apple doc

UNNotificationServiceExtension An object that modifies the content of a remote notification before it's delivered to the user.



回答4:

Notification Extension is supported for local notifications too. It is clearly mentioned here

UNNotificationContentExtension An object that presents a custom interface for a delivered local or remote notification.