履行

  1. 在 xCode Navigator 窗口中转到 Targets 部分。按 Add New Target。
  2. 选择 Notification Content Extension 模板:

https://i.stack.imgur.com/5EUot.jpg

  1. 在你的 info.plist 文件中设置 UNNotificationExtensionCategory 密钥的标识符:

https://i.stack.imgur.com/nI8ck.jpg

NSExtensionAttributes :

UNNotificationExtensionCategory(必填)

此键的值是字符串或字符串数​​组。每个字符串都包含应用程序使用 UNNotification Category 类声明的类别的标识符。

UNNotificationExtensionInitialContentSizeRatio(必填)

表示视图控制器视图初始大小的数字,表示为高度与宽度的比率。

UNNotificationExtensionDefaultContentHidden(可选)

设置为 YES 时,系统仅在通知界面中显示自定义视图控制器。设置为 NO 时,除视图控制器的内容外,系统还会显示默认通知内容。

UNNotificationExtensionOverridesDefaultTitle(可选)

该键的值是布尔值。设置为 true 时,系统使用视图控制器的 title 属性作为通知的标题。设置为 false 时,系统会将通知的标题设置为应用程序的名称。如果未指定此键,则默认值设置为 false。

  1. 在 NotificationViewController.swift 文件中创建自定义视图
  2. 添加新的 category key 并将其值设置为我们在 Info.plist 中输入的内容(步骤 3):

推:

{
 aps: {
 alert: { … },
 category: 'io.swifting.notification-category' 
 }
}

本地:

let mutableNotificationContent = UNMutableNotificationContent()
mutableNotificationContent.category = "io.swifting.notification-category"
mutableNotificationContent.title = "Swifting.io Notifications"
mutableNotificationContent.subtitle = "Swifting.io presents"
mutableNotificationContent.body = "Custom notifications"

另请参阅官方 API 参考: https : //developer.apple.com/reference/usernotificationsui/unnotificationcontentextension?utm_source = swing.io&utm_medium = web&utm_campaign = blog%20post