CTNotificationContent
用于显示 iOS 10 推送通知的个性化内容接口的 Notification Content 扩展类
从 iOS 10 开始,您可以为 iOS 推送通知添加自定义内容视图。这个库提供了一个类来完成这项工作。它提供了一个默认的图片幻灯片视图,并设计得易于扩展以显示其他视图类型。
通过 iOS 10 中的 自定义推送通知内容接口启用,这是一个嵌入到您的应用程序包中的独立且独立的二进制文件,它通过Notification Content Extension实现。
目录
🎉 安装
CocoaPods
您的Podfile应类似于以下内容
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target 'YOUR_NOTIFICATION_CONTENT_TARGET_NAME' do
pod 'CTNotificationContent'
end
然后运行pod install
Swift Package Manager
Swift Package Manager 是Xcode的一个工具,用于安装项目依赖项。要使用它来安装CTNotificationContent SDK,请按照以下步骤操作
- 在Xcode中,转到 文件 -> Swift Package Manager -> 添加包依赖。
- 选择包仓库时输入 https://github.com/CleverTap/CTNotificationContent.git,然后点击 下一步。
- 在下一屏幕上,选择一个SDK版本(默认情况下,Xcode选择最新稳定版)。点击 下一步。
- 点击 完成 并确保已在适当的目标中添加了 CTNotificationContent。
🚀 设置
配置您的应用程序以推送通知并添加通知内容扩展目标
在您的主应用程序中启用 推送通知。
在您的项目中创建一个通知内容扩展。要在Xcode项目中完成此操作,请选择 File -> New -> Target 并选择 Notification Content Extension 模板。
将通知内容扩展配置为使用 CTNotificationViewController 类
将 NotificationViewController 的基类更改为 CTNotificationViewController
。您不应该在 NotificationViewController 类中实现任何 UNNotificationContentExtension 协议方法,这些方法将由 CTNotificationViewController
处理。请在此处查看 Objective-C 示例(见此处)和 Swift 示例(见此处)。
在通知内容目标中编辑 Maininterface.storyboard
为 plain UIView,请见示例。
在您的 AppDelegate 中注册通知类别和操作
Swift
// register category with actions
let action1 = UNNotificationAction(identifier: "action_1", title: "Back", options: [])
let action2 = UNNotificationAction(identifier: "action_2", title: "Next", options: [])
let action3 = UNNotificationAction(identifier: "action_3", title: "View In App", options: [])
let category = UNNotificationCategory(identifier: "CTNotification", actions: [action1, action2, action3], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
Objective-C
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action_1" title:@"Back" options:UNNotificationActionOptionNone];
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"action_2" title:@"Next" options:UNNotificationActionOptionNone];
UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action_3" title:@"View In App" options:UNNotificationActionOptionNone];
UNNotificationCategory *cat = [UNNotificationCategory categoryWithIdentifier:@"CTNotification" actions:@[action1, action2, action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
[center setNotificationCategories:[NSSet setWithObjects:cat, nil]];
在此查看Objective-C示例和在此查看Swift示例。
然后配置您的通知内容目标Info.plist文件以反映您注册的类别标识符:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory
。此外,设置UNNotificationExtensionInitialContentSizeRatio -> 0.1
和UNNotificationExtensionDefaultContentHidden -> true
。
此外,如果您计划下载非SSL URL,请确保在您的清单中启用App Transport Security Settings -> Allow Arbitrary Loads -> true
。 在此查看plist示例。
仪表板使用
(返回顶部) 在CleverTap上创建推送通知活动时,请按照以下步骤操作:
- 在“内容”部分,在“标题”和“消息”字段中传递所需的值(注意:这些是iOS警报标题和正文)。
- 点击“高级”,然后点击“富媒体”,并选择单张或轮播模板。《a target="_blank" rel="noopener noreferrer" href="https://github.com/CleverTap/CTNotificationContent/blob/master/images/dashboard_richMedia.png">
- 若要添加自定义键值对,请单独或在一个JSON对象中添加模板键,并使用
pt_json
键填写值。 - 发送测试推送并及时安排!
模板类型
富媒体
单个媒体
内容滑块
内容滑块允许用户添加带不同标题、子标题和动作的多张图片,用于图片幻灯片视图。
自定义键值对
基本模板
基本模板是指用户在应用程序中接收到的基本推送通知,用户还可以更新文本颜色和背景颜色。
自动轮播模板
自动轮播是一种自动旋转的轮播推送通知,用户也可以更新文本颜色和背景颜色。
手动轮播模板
这是手动版轮播图。用户可以通过点击“下一页/上一页”按钮来浏览下一张/上一张图片。
注意
对于iOS 12及更高版本,您需要配置您的通知内容目标Info.plist来反映您已注册的类别标识符:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory
。此外,设置UNNotificationExtensionInitialContentSizeRatio -> 0.1
,UNNotificationExtensionDefaultContentHidden -> true
和UNNotificationExtensionUserInteractionEnabled -> 1
。
对于iOS 11及以下版本,下一页/上一页按钮将不起作用。请使用具有标识符action_1
和action_2
的通知动作来完成此操作。
计时器模板
此模板具有实时倒计时计时器。计时器完成后,您还可以选择显示不同的标题、信息和背景图片。
无边框模板
无边框模板确保背景图片覆盖推送通知的整个可用视觉区域。所有文本都叠加在图片上。
评分模板
评分模板允许用户为您提供反馈。
注意
对于iOS 12及更高版本,您需要配置您的通知内容目标Info.plist以反映您已注册的类别标识符:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory
。此外,设置UNNotificationExtensionInitialContentSizeRatio -> 0.1
、UNNotificationExtensionDefaultContentHidden -> true
和UNNotificationExtensionUserInteractionEnabled -> 1
。对于iOS 11及以下版本,将回退到基本模板。
产品目录模板
产品目录模板允许您在用户点击“立即购买”选项之前,展示不同产品(或产品目录)的图片。此模板有两种变体。
注意
对于iOS 12及更高版本,您需要配置您的通知内容目标Info.plist以反映您已注册的类别标识符:NSExtension -> NSExtensionAttributes -> UNNotificationExtensionCategory
。此外,设置UNNotificationExtensionInitialContentSizeRatio -> 0.1
、UNNotificationExtensionDefaultContentHidden -> true
和UNNotificationExtensionUserInteractionEnabled -> 1
。对于iOS 11及以下版本,将回退到基本模板。
垂直视图
线性视图
使用以下键启用此模板的线性视图变体。
模板键 | 必填 | 值 |
---|---|---|
pt_product_display_linear | 可选 | true |
Web视图模板
Web视图模板允许您加载远程的https URL。
注意:如果任何图片无法下载,模板将回退到只有标题和副标题的基本模板。
模板键
丰富媒体
内容滑块
配置您的APNS有效负载
然后,当通过APNS发送通知时
- 在您的有效负载aps条目中包含可变内容标志(此键必须存在于aps有效负载中,否则系统不会调用您的应用扩展)
- 对于图片轮播视图,将具有JSON对象值的
ct_ContentSlider
键添加到有效负载中,除aps条目之外,请参见下面的示例。
{
"aps": {
"alert": {
"body": "test message",
"title": "test title",
},
"category": "CTNotification",
"mutable-content": true,
},
"ct_ContentSlider": {
"orientation": "landscape", // landscape assumes 16:9 images, remove to display default square/portrait images
"showsPaging": true, // optional to display UIPageControl
"autoPlay": true, // optional to auto play the slideshow
"autoDismiss": true, // optional to auto dismiss the notification on item actionUrl launch
"items":[
{
"caption": "caption one",
"subcaption": "subcaption one",
"imageUrl": "https://s3.amazonaws.com/ct-demo-images/landscape-1.jpg",
"actionUrl": "com.clevertap.ctcontent.example://item/one"
},
{
"caption": "caption two",
"subcaption": "subcaption two",
"imageUrl": "https://s3.amazonaws.com/ct-demo-images/landscape-2.jpg",
"actionUrl": "com.clevertap.ctcontent.example://item/two"
}
]
}
}
自定义键值对
基本模板
基本模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_basic |
pt_title | 必填 | 标题 |
pt_msg | 必填 | 消息 |
pt_msg_summary | 必填 | 通知展开时的消息行 |
pt_bg | 必填 | 背景颜色(十六进制表示) |
pt_big_img | 可选 | 图像 |
pt_dl1 | 可选 | 一个深度链接 |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
自动轮播模板
自动轮播模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_carousel |
pt_title | 必填 | 标题 |
pt_msg | 必填 | 消息 |
pt_msg_summary | 可选 | 通知展开时的消息行 |
pt_dl1 | 必填 | 深度链接 |
pt_img1 | 必填 | 第一张图像 |
pt_img2 | 必填 | 第二张图像 |
pt_img3 | 必填 | 第三张图像 |
pt_bg | 必填 | 背景颜色(十六进制表示) |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
手动轮播模板
手动轮播模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_manual_carousel |
pt_title | 必填 | 标题 |
pt_msg | 必填 | 消息 |
pt_msg_summary | 可选 | 通知展开时的消息行 |
pt_dl1 | 必填 | 深度链接1 |
pt_img1 | 必填 | 第一张图像 |
pt_img2 | 必填 | 第二张图像 |
pt_img3 | 必填 | 第三张图像 |
pt_bg | 必填 | 背景颜色(十六进制表示) |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
计时器模板
计时器模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_timer |
pt_title | 必填 | 标题 |
pt_title_alt | 可选 | 计时结束后显示的标题 |
pt_msg | 必填 | 消息 |
pt_msg_alt | 可选 | 计时结束后显示的消息 |
pt_msg_summary | 可选 | 通知展开时的消息行 |
pt_dl1 | 必填 | 深度链接 |
pt_big_img | 可选 | 图像 |
pt_big_img_alt | 可选 | 计时结束后显示的图片 |
pt_bg | 必填 | 背景颜色(十六进制表示) |
pt_chrono_title_clr | 可选 | 计时器文本颜色(十六进制) |
pt_timer_threshold | 必填 | 计时持续时间(秒)。将获得更高的优先级。 |
pt_timer_end | 可选 | 倒计时结束的纪元时间戳(例如,$D_1595871380 或 1595871380)。如果指定了 pt_timer_threshold,则不需要。 |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
零边框模板
零边框模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_zero_bezel |
pt_title | 必填 | 标题 |
pt_msg | 必填 | 消息 |
pt_msg_summary | 可选 | 通知展开时的消息行 |
pt_subtitle | 可选 | 副标题 |
pt_big_img | 必填 | 图像 |
pt_dl1 | 必填 | 深度链接 |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
评分模板
评分模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_rating |
pt_title | 必填 | 标题 |
pt_msg | 必填 | 消息 |
pt_big_img | 可选 | 图像 |
pt_msg_summary | 可选 | 通知展开时的消息行 |
pt_subtitle | 可选 | 副标题 |
pt_default_dl | 必填 | 推送通知的默认深链接 |
pt_dl1 | 必填 | 第一个/所有星星的深链接 |
pt_dl2 | 可选 | 第二个星星的深链接 |
pt_dl3 | 可选 | 第三个星星的深链接 |
pt_dl4 | 可选 | 第四个星星的深链接 |
pt_dl5 | 可选 | 第五个星星的深链接 |
pt_bg | 必填 | 背景颜色(十六进制表示) |
pt_ico | 可选 | 大图标 |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
产品目录模板
产品目录模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_product_display |
pt_title | 必填 | 标题 |
pt_msg | 必填 | 消息 |
pt_subtitle | 可选 | 副标题 |
pt_img1 | 必填 | 第一张图像 |
pt_img2 | 必填 | 第二张图像 |
pt_img3 | 可选 | 第三张图像 |
pt_bt1 | 必填 | 第一张图片的大文本 |
pt_bt2 | 必填 | 第二张图片的大文本 |
pt_bt3 | 必填 | 第三张图片的大文本 |
pt_st1 | 必填 | 第一张图片的小文本 |
pt_st2 | 必填 | 第二张图片的小文本 |
pt_st3 | 必填 | 第三张图片的小文本 |
pt_dl1 | 必填 | 第一张图片的深链接 |
pt_dl2 | 必填 | 第二张图片的深链接 |
pt_dl3 | 必填 | 第三张图片的深链接 |
pt_price1 | 必填 | 第一张图片的价格 |
pt_price2 | 必填 | 第二张图片的价格 |
pt_price3 | 必填 | 第三张图片的价格 |
pt_bg | 必填 | 背景颜色(十六进制表示) |
pt_product_display_action | 必填 | 操作按钮标签文本 |
pt_product_display_linear | 可选 | 线性布局模板 ("true"/"false") |
pt_product_display_action_clr | 必填 | 操作按钮背景颜色(十六进制) |
pt_title_clr | 可选 | 标题颜色(十六进制表示) |
pt_msg_clr | 可选 | 消息颜色(十六进制表示) |
pt_json | 可选 | 以上键以JSON格式表示 |
WebView 模板
WebView 模板键 | 必填 | 描述 |
---|---|---|
pt_id | 必填 | 值 - pt_web_view |
pt_dl1 | 必填 | 深度链接 |
pt_url | 必填 | 要加载的URL |
pt_orientation | 可选 | 值 - landscapes 或 portraits |
pt_json | 可选 | 以上键以JSON格式表示 |
示例应用
-
在此处查看 Swift 项目的示例。
-
在此处查看 Objective-C 项目的示例。
-
在此处查看 SwiftPM 项目的示例。