一个应用程序中的多个通知服务扩展

时间:2016-12-29 10:38:48

标签: ios objective-c iphone swift

是否可以在一个应用中添加多个通知服务扩展?如果是,那么如何识别将使用哪一个以及如何使用?

基本上我的应用程序有两个服务提供商,它们都有自己的通知服务扩展的有效负载,所以我可以通过哪种方式添加两个不同的通知服务扩展,并根据serviceProvider的有效负载值==“ 1“我可以告诉app运行serviceProvider 1的扩展

1 个答案:

答案 0 :(得分:2)

NotificationServiceExtension

文档没有对此说任何话。在我的测试中,它不起作用。所有通知都通过单个NotificationServiceExtension处理。

NotificationContentExtension

对于NotificationContentExtension,文档说:

您可以将多个通知内容应用扩展添加到您的 项目,但每个人都必须支持一组唯一的通知 类别。您可以在应用程序扩展中为其指定类别 Info.plist文件,如声明支持的通知中所述 类型。

Customizing the Appearance of Notifications docs

我验证了☝️并且有效!

还值得一提的是,NotificationServiceExtension的默认plist设置如下所示:

enter image description here

它没有将自己与任何给定的类别相关联。我尝试添加NSExtensionAttributes和一个UNNotificationCategoryExtension键值。但是即使编译了,也没有用!我认为Apple决定如何使用Notification Service Extension的方式基于以下两个字段:

  • 其bundleID前缀为apns-topic的目标
  • 一个NSExtensionPointIdentifer字段,必须始终将其设置为com.apple.usernotifications.service。对于今天的扩展名或内容通知扩展名等,此值是不同的。

因此,如果您有两个服务扩展名,那么系统将无法决定应显示哪个扩展名

不过,NotificationContentExtension的默认plist设置确实具有UNNotificationCategoryExtension键,但包含以下值:

enter image description here


还要多考虑这一点,如果一个应用程序有5个不同的类别,并且每个类别都有一个服务扩展,并且一次接收到所有这些类别,那么它将启动5个不同的进程(认为5个并行{{1} }回调。每个类别和每个进程都有一个回调),这对操作系统不利。

虽然不确定,documentation on Signal's NotificationService class支持这一理论。

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. 并非如此。它不能一次处理5个contentExtensions。因为它是一个由主线程控制的UI功能。