WEContentExtension 是集成在 WebEngage 中的扩展 SDK,用于支持在 iOS 应用程序内丰富的推送布局
点击展开
- 需要在项目中集成 WebEngage SDK
- 了解服务扩展和内容扩展的基本知识
- 了解推送通知,Swift / Objective-C 编程语言的基本知识
-
本指南提供将内容扩展集成到您的 iOS 项目中的逐步指南。内容扩展允许您通过提供可在各种上下文中显示的附加内容来扩展应用程序的功能,例如 Today 视图或 Messages 应用程序。
-
打开您的 Xcode 项目。
-
从菜单中选择
File
>New
>Target...
。 -
从列表中选择
App Extension
模板。 -
选择您要创建的扩展类型(例如,ContentExtension)。
-
为您的扩展提供一个名称并点击
Finish
。
-
-
在内容扩展中集成库有两种常见方法
建议选择 Swift Package Manager 或 Cocoapods 将库集成到您的内容扩展中。同时使用这两种方法可能会导致项目设置中的冲突或不一致。
-
- 转到
File
>Swift Packages
>Add Package Dependency...
。
-
输入库仓库的 URL:
https://github.com/WebEngage/WEContentExtension
。 -
单击
Next
。 -
选择要使用的分支或输入特定的分支要求(master)。
-
单击
Next
。
-
选择要将依赖项添加到的目标,即您的 Content Extension 目标。
-
单击
Finish
。
- 转到
-
-
应将 Cocoapods 安装在您的系统上
-
为您的项目应可用的 podfile
-
-
-
使用文本编辑器打开 Podfile。
-
将库依赖项添加到 Podfile 中。例如
# this target name should be your ContentExtension Name target 'ContentExtension' do pod 'WEContentExtension' end
注意:您的目标名称应为在创建 ContentExtension 时输入的内容扩展名称,在此处参考截图 3
-
-
-
保存对 Podfile 的更改。
-
运行以下命令安装 pods:
pod install
-
-
-
-
-
打开 NotificationViewController.swift
-
通过添加代码
import WEContentExtension
来导入WEContentExtension -
将类
NotificationViewController
中所有现有的代码移除 -
使用
WEXRichPushNotificationViewController
对NotificationViewController
进行子类化
NotificationViewController.swift
将呈现如下代码片段import UIKit import UserNotifications import UserNotificationsUI // Import WebEngage Extension SDK import WEContentExtension // Subclassing current class with WebEngage provided class class NotificationViewController: WEXRichPushNotificationViewController { // remove all existing code inside this class }
-
-
-
打开 NotificationViewController.m
-
导入
WEContentExtension
-
创建一个
WEContentExtension
对象 -
通过上述创建的对象将必要的信息传递给WebEngage
NotificationViewController.m
将呈现如下代码片段#import "NotificationViewController.h" #import <UserNotifications/UserNotifications.h> #import <UserNotificationsUI/UserNotificationsUI.h> // Step 1 : Importing WEContentExtension #import <WEContentExtension/WEContentExtension-Swift.h> @interface NotificationViewController () <UNNotificationContentExtension> @property IBOutlet UILabel *label; // Step 2 : Creating Object of content Extension @property WEXRichPushNotificationViewController *weRichPushVC; @end @implementation NotificationViewController // Step 3 : Pass necessary information to WebEngage - (void)viewDidLoad { if (_weRichPushVC == NULL){ _weRichPushVC = [[WEXRichPushNotificationViewController alloc]init]; } [_weRichPushVC setUpViewsWithParentVC:self]; [super viewDidLoad]; } - (void)didReceiveNotification:(UNNotification *)notification { [_weRichPushVC didReceiveNotification:notification]; } - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion{ [_weRichPushVC didReceiveNotificationResponse:response completionHandler:completion]; } @end
-
-
-
以下是操作方法
-
打开针对
NotificationViewController
的Info.plist
文件 -
展开
NSExtension
>NSExtensionAttributes
-
在
NSExtensionAttributes
中查找 UNNotificationExtensionCategory。如果不存在,请添加它,并将类型设置为 Array。在Array中添加以下项:- WEG_CAROUSEL_V1
- WEG_RATING_V1
- WEG_RICH_V1
- WEG_RICH_V2
- WEG_RICH_V3
- WEG_RICH_V4
- WEG_RICH_V5
- WEG_RICH_V6
- WEG_RICH_V7
- WEG_RICH_V8
-
将
UNNotificationExtensionDefaultContentHidden
下的属性设置为YES
-
在NotificationViewController的
Info.plist
文件中的信息属性列表下添加App Transport Security设置键。在这两个文件中,都将App Transport Security设置下的Allow Arbitrary Loads设置为YES
。(注意:如果您确定WebEngage仪表板上的推送通知提供的图像URL始终使用https,则上述步骤不是必需的。)
-
Info.plist
的源代码片段<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <array> <string>WEG_CAROUSEL_V1</string> <string>WEG_RATING_V1</string> <string>WEG_RICH_V1</string> <string>WEG_RICH_V2</string> <string>WEG_RICH_V3</string> <string>WEG_RICH_V4</string> <string>WEG_RICH_V5</string> <string>WEG_RICH_V6</string> <string>WEG_RICH_V7</string> <string>WEG_RICH_V8</string> </array> <key>UNNotificationExtensionDefaultContentHidden</key> <true/> <key>UNNotificationExtensionInitialContentSizeRatio</key> <real>1</real> </dict> <key>NSExtensionMainStoryboard</key> <string>MainInterface</string> <key>NSExtensionPointIdentifier</key> <string>com.apple.usernotifications.content-extension</string> </dict>
-
Info.plist
应该看起来像以下截图
-
-
App Groups允许当接收到通知时,即使您的应用程序未处于活动状态,您的应用程序和WebEngageNotificationContentExtension之间的通信。这对于确认投递是必需的。
-
-
构建您的项目以确保库成功集成。
-
测试您的内容扩展以确保它使用集成库按预期工作。
-
-
-
如果您一直在使用旧内容扩展并希望切换到新的内容扩展,请始终遵循文档中的这些说明
-
以下是从WebEngageAppEx迁移到WEContentExtension的步骤:
-
从podfile中删除
pod 'WebEngageAppEx/ContentExtension'
中的 Content Extension TargetNotificationViewController
。 -
然后执行
pod install
-
然后按照第2种方法:通过Cocoapods集成进行操作
-
完成以上步骤后,让我们进入代码部分
-
- 打开
NotificationViewController.swift
文件。 - 将
import WebEngageAppEx
替换为import WEContentExtension
。 - 完成了
- 打开
-
- 打开
NotificationViewController.h
文件。 - 移除导入语句
#import
。 - 将
WEXRichPushNotificationViewController
替换为UNNotificationContentExtension
。 - 打开
NotificationViewController.m
文件。 - 如第Objective-C步所述替换代码
- 打开
-
-
-
WEContentExtension可在MIT许可证下使用。更多信息请参阅LICENSE文件。