一个应用程序中是否可以添加多个通知服务扩展?如果可以的话,如何识别将使用哪一个并且怎么做呢?
基本上我的应用有两个服务提供商,他们都有自己的通知服务扩展的有效负载,那么是否有办法可以添加两个不同的通知服务扩展,并根据serviceProvider ==“1”的有效负载值告诉应用运行serviceProvider 1的扩展?
一个应用程序中是否可以添加多个通知服务扩展?如果可以的话,如何识别将使用哪一个并且怎么做呢?
基本上我的应用有两个服务提供商,他们都有自己的通知服务扩展的有效负载,那么是否有办法可以添加两个不同的通知服务扩展,并根据serviceProvider ==“1”的有效负载值告诉应用运行serviceProvider 1的扩展?
文档中没有提到这个。在我的测试中它并不起作用。所有通知都会通过一个单一的 NotificationServiceExtension
处理。
对于NotificationContentExtension
,文档说:
您可以向项目中添加多个通知内容应用程序扩展,但每个扩展必须支持唯一的通知类别集。您可以在其Info.plist文件中指定应用程序扩展的类别,如声明支持的通知类型所述。
我进行了验证 ☝️,它有效!值得一提的是,您可以使用单个通知内容扩展来处理多个类别。
UNNotificationExtensionCategory(必需)
一个字符串或字符串数组。每个字符串包含由应用程序使用UNNotificationCategory类声明的类别的标识符。
还值得一提的是,NotificationServiceExtension
的默认 plist 设置如下:
NSExtensionAttributes
和一个UNNotificationCategoryExtension
键值对。但是,即使编译通过,它也没有起作用!我认为Apple决定如何使用通知服务扩展是基于这两个字段:
apns-topic
为前缀的目标NSExtensionPointIdentifer
字段必须始终设置为com.apple.usernotifications.service
。该值对于今天的扩展或内容通知扩展等不同。因此,如果您有两个服务扩展,则系统无法确定应显示哪一个
然而,默认的NotificationContentExtension
的plist设置确实包括UNNotificationCategoryExtension
键值对:
didFinishLaunchingWithOptions
回调。一个用于每个类别和进程),这对于操作系统来说是不好的。
// 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.
< p > 对于 NotificationContentExtension
来说,情况并非如此。它无法同时处理 5 个 contentExtensions。因为它是一个由主线程控制的 UI 功能。