Multiple Notification Service Extension in one app
Asked Answered
J

1

10

Is it possible to add Multiple Notification Service Extension in one app?If Yes, then how to recognize which one will be used and how?

Basically there are two service providers for my app and both of them have their own payload for notification service extension so is there any way by which I can add two different notification service extension and according to value in payload of serviceProvider == "1" I can tell app to run the Extension of serviceProvider 1

Jelena answered 29/12, 2016 at 10:38 Comment(2)
Have you found your answer ?Justiciable
@Justiciable FYI I did add an answer a while backSorb
S
6

NotificationServiceExtension

The docs doesn't say anything about that. In my tests it didn't work. All notifications were getting processed through a single NotificationServiceExtension.

NotificationContentExtension

For NotificationContentExtension the docs say:

You may add more than one notification content app extension to your project, but each one must support a unique set of notification categories. You specify the categories for your app extension in its Info.plist file, as described in Declare the Supported Notification Types.

Customizing the Appearance of Notifications docs

I verified ☝️ and it worked! FWIW you can use a single a Notification Content Extension for multiple categories.

UNNotificationExtensionCategory (Required)

A string or an array of strings. Each string contains the identifier of a category declared by the app using the UNNotificationCategory class.

What's also worth mentioning is that the default plist settings of a NotificationServiceExtension looks like this:

enter image description here

It's not associating itself with any given category. I tried adding the NSExtensionAttributes, along with a UNNotificationCategoryExtension key, value. But even though it compiled, it didn't work!. I think the way Apple decides how to use a Notification Service Extension is based off these two fields:

  • A target that its bundleID is prefixed with as the apns-topic
  • An NSExtensionPointIdentifer field which must always be set to com.apple.usernotifications.service. The value of this is different for Today's extension or content notification extension, etc.

So if you have two service extensions then the system has no way of deciding which one it should show

However the default plist settings of a NotificationContentExtension does have UNNotificationCategoryExtension key, value included:

enter image description here


Also thinking about this more, if an app has 5 different categories and has a service extension for each each of them and receives them all at once, then it would fire up 5 different processes (think of 5 parallel didFinishLaunchingWithOptions callbacks. One for each category and process) and that's bad for the OS.

While not certain, the documentation on Signal's NotificationService class supports this theory.

// Note that the NSE does *not* always spawn a new process to
// handle a new notification and will also try and process notifications
// in parallel. `didReceive` could be called twice for the same process,
// but will always be called on different threads. To deal with this we
// ensure that we only do setup *once* per process and we dispatch to
// the main queue to make sure the calls to the message fetcher job
// run serially.

The same isn't true for NotificationContentExtension. It can't process 5 contentExtensions at once. Because it's a UI featured which is gated by the main thread.

Sorb answered 20/2, 2020 at 19:40 Comment(5)
Yes you are right NotificationServiceExtension goes through single payload and I solved my use case based in NotificationService Extension by checking the payload and then performing task.Jelena
doesnt work for me.. the NotificationServiceExtension used is always the first one i've created. Any suggestions?Hostler
@Hostler what doesn't work for you? Do you have multiple NotificationServiceExtensions?Sorb
@Honey yes, i've 2 notificationExtensions, each one has a different notificaiton provider. The first one has A provider, the second one the provider B. Each plist is built with UNNotificationExtensionCategory key. I send the push with "category" key inside the aps (value = to UNNotificationExtensionCategory value). The service called is always the first one (A); the second notification service is never called.Hostler
@honey misunderstanding on my side. Thank you 😉 solved using notification category name.Hostler

© 2022 - 2024 — McMap. All rights reserved.