EngageSDK
欢迎使用 Engage SDK 用户指南。在此,您可以找到所有使用 Engage SDK 每个功能的代码片段和所需内容。您还可以在此处查看 SDK 文档(此处)。让我们开始吧。
在您的项目中添加 Engage
将 Engage SDK 添加到 iOS 项目非常简单。打开配置文件,并将以下代码添加进去。
pod 'EngageSDK'
只需在终端中输入 pod install,然后即可设置完成。您可以在此处找到 SDK 的文档。按照此用户指南开始使用 SDK。
要求
iOS 11.0+
Xcode 10.3
注意 -> 支持 xcode 10.3 及以下版本
初始化
在使用 SDK 之前需要对其进行初始化。请注意,为了使用此 SDK,必须在使用它之前进行初始化,否则将引发 Error
。初始化此 SDK 需要
- ProximiPro API 密钥:由 Engage 平台提供
- 客户端 ID:由 Engage 平台提供
- 区域标识符:(用户自定义)
- UUID:由 Engage 平台提供
- 项目名称/应用程序名称:用作从SDK显示的所有通知的通知标题。
为了首次初始化此SDK,需要互联网连接,因为它会检查并验证API密钥与服务器。没有互联网连接无法初始化SDK。一旦通过互联网验证API密钥,随后运行/初始化不再需要互联网连接。
// Create Initialization request
let initializationRequest = InitializationRequest(
apiKey: "API_KEY",
appName: "App_Name",
regionId: "REGION_IDENTIFIER",
clientId: "CLIENT_ID",
uuid: "UUID"
)
// InitializeEngage
let _ = EngageSDK.init(initData: initializationRequest,
onSuccess: {
// SDK initialized
}) { (errorMessage) in
// Error initializing SDK
}
检查SDK是否已初始化
一旦验证API密钥,SDK将在应用启动时自动初始化。无需每次都重新初始化。以下是检查SDK是否已初始化的方法:
guard let engage = EngageSDK.shared else { return }
if engage.isInitialized { /* SDK is initialized */ }
用法
要使用SDK,有一个操作的单入口点。入口点是Engage
类。以下是如何获取SDK的单例实例:
// returns SDK instance if it's initialized, throws nil otherwise
guard let engage = EngageSDK.shared else { return }
注册用户
要使用SDK注册用户,需要以下两个信息:
- 出生日期
birthDate: String, dateFormat = "yyyy/MM/dd"
- 性别
gender:Gender
: 1.Gender.Male
2.Gender.Female
guard let engage = EngageSDK.shared else { return }
engage.callRegisterUserApi(birthDate: BirthDate, gender: Gender)
{ (response) in
if let _ = response {
// User Registered successfully
} else {
// Unable to register User
}
}
标签
用户注册后,可以像这样检索与账户相关的标签:
获取用户默认标签
guard let engage = EngageSDK.shared else { return }
var tags = engage.userInfo?.tags ?? [Tag]()
修改用户出生日期、性别和标签
这些标签可供选择,用户可以选择自己的兴趣。一旦选择完成,需要与服务器同步。为此,可以使用以下方式的 callUpdateUserApi
API:
guard let engage = EngageSDK.shared else { return }
engage.callUpdateUserApi(birthDate: text, gender: gender, tags: tags) { (response) in
if let response = response {
// User Updated successfully
} else {
// Unable to update User
}
}
获取更新后的用户信息
用户成功使用 SDK 注册后,可以像这样从 engage 配置对象中检索所有相关用户信息:
guard let engage = EngageSDK.shared else { return }
val birthDate = engage.userInfo?.birthDate
val gender = engage.userInfo?.gender
val tags = engage.userInfo?.tags
启动和停止扫描
启动扫描
此 SDK 支持生命周期扫描,意味着只有当应用程序在前台时才会传递扫描结果。此外, SDK 会根据触发的规则显示通知。
请注意, SDK 使用前台服务进行信标扫描。每次开始扫描时,此服务将显示通知,扫描停止时将移除通知。请参阅 配置服务通知 部分,以修改默认通知设置。
engage.start{...}
调用方法需要定位权限以启动扫描,因为底层本地模块需要此权限进行扫描。在开始扫描之前,请确保应用程序具有定位权限,否则扫描过程不会开始,也不会提供任何扫描结果。
// start scan
guard let engage = EngageSDK.shared else { return }
// assign location manager object to sdk before start scaning
if engage.locationManager == nil {
engage.locationManager = locationManager
}
// if user don't allow the location, notifiation and bluetooth start method return with error message
engage.start { (message, permission) in
// message - error message
// permission- true/false
// other wise your scan is started
engage.onBeaconCamped = { beacon, location in
// called on beacon detection
}
engage.onBeaconExit = { beacon, location in
// called when beacon exit
}
engage.onRangedBeacon = { beacons in
// List of detected beacons
}
engage.onRuleTriggeres = { rule, location in
// called when a rule is triggered upon beacon detection
}
engage.onLocationRuleTriggeres = { rule, location in
// called when a rule is triggered upon location detection
}
engage.onPermissionChange = { (message, permission) in
if !permission {
// if user change any permission then this block called
}
}
engage.locationCheckLiveData = { location in
if let location = location {
// Called when a SDK search for the location based content
}
}
}
停止扫描
停止正在进行的扫描很简单。它将清除所有后台和前台扫描监听器。
engage.stop()
服务通知中有一个停止扫描
按钮,用于停止正在进行的扫描过程。由于此通知作为前台服务的部分显示,因此无法将其取消,直到服务被停止。
后台模式
此SDK支持后台扫描模式,即使在应用不在前台的情况下也能继续扫描。它会在扫描结果上显示通知,并通过这些通知回到主应用。如果开启了后台模式,设备开机时会自动开始扫描。
开启后台模式
engage.isBackgroundModeEnabled = true
engage.isNotificationEnabled = true
为了开启后台扫描,需要将
isBackground
和isNotificationEnabled
都设置为true。如果其中一个没有被设置为true,则后台扫描不会启动。请注意,为了应用这些更改,无需重新启动扫描过程,因为它对于其他设置是必须的。
基于位置的文本
基于位置的文本功能使SDK能够使用设备位置并获取相关的数据。为了启用基于位置的文本
engage.isLocationBasedContentEnabled = true
使用内容列表视图
SDK提供列表视图,在应用在前台时加载所有支持的内容。使用这个ContentListView
非常简单。
首先在你的UITableViewCell中添加视图
将ListTableViewCell类分配给UITableViewCell,并将WKWebView连接到出口:https://ibb.co/Kj4jDsY
将WkWebView分配给webview:https://ibb.co/8gk3jph
设置视图内容
一旦你从 onRuleTriggered
方法接收到规则,你可以创建 Content
类的实例,并创建一个列表。然后你只需要将内容列表传递给视图,它将从服务器加载数据。
cell.loadContent(data: contents[indexPath.row]) // where contents is [Content]
使用 ContentDetailView
SDK 提供了 ContentView
来加载数据列表,同时也提供了内容的详细信息视图。使用 ContentView
非常简单。将 ContentView
类分配给 UIView
ContentView
中加载数据
在 你可以将 Content
实例从一个组件传递过来。
contentView.loadContent(content: content)
处理通知
在扫描过程开始后,SDK 会根据触发的规则显示通知。点击这些通知时,默认会启动应用程序。
// iOS App default methods to get notification
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let notificationData = notification.request.content.userInfo
completionHandler([.alert, .sound])
// Called when notification arrive
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Notification Clicked")
// Called when notification clicked
}
从点击的通知中检索信息
你需要将 userInfo
直接传递给 SDK 以获取内容。
// Before call this method EngageSDK must be initialized.
func handleNotification(userInfo: [AnyHashable: Any]) {
EngageSDK.shared?.onPromotion = { promostionAction in
print("On Promotion \(promostionAction)")
EngageSDK.shared?.callPromotionApi(id: promostionAction.meta?.params?.id ?? "", responseData: { (response) in
// user will get content here
})
}
EngageSDK.shared?.handleNotification(userInfo: userInfo, isWebContent: {
// Called when notification contain web content
})
}
从 SDK 中获取 FCM 通知
从 FCM 获取通知后,你只需从 firebase 获取令牌并将其传递给 SDK。
engage.callPushNotificationRegister(pushToken: "FCM Token", responseData: { (response) in
// do nothing
})
更新信标UUID & API密钥
SDK使用ProximiPRO Engage平台提供默认的信标UUID。但是,如果需要更改变量UUID,可以非常容易地做到。
请确保UUID格式正确且无误。另外,为了使SDK应用更改,需要重新启动应用程序。
SDK提供更改SDK使用的API密钥的方法。
更改API密钥后,SDK将在下一次运行时重新验证它,以确保它是有效的API密钥。如果无法验证新API密钥,SDK将不会初始化。此外,为了使这一更改生效,应用程序需要重新启动。
// returns true if the uuid is updated successfully, false otherwise
Re-initialized the SDK
延迟通知与内容
Engage SDK支持您轻松延期点击的通知和不点击的通知内容。
延期未点击通知
engage.snoozeNotificationTimeInMinutes = Minutes
延期内容
engage.snoozeContentTimeInHours = Hours
TxPower
在iOS中,不提供TxPower于信标,SDK为了距离计算提供了添加TxPower的选项。
engage.txPower = txPower_value : `https://ibb.co/hfG0bQZ`
日志
要启用事件日志SDK,请提供日志方法
FCM日志
推送通知日志
guard let engage = EngageSDK.shared else { return }
engage.callLogPushNotification(notificationId: notifiaction id "id", action: action like "open" or "click") { (response) in
print(response ?? "")
}
事件日志
在事件日志中,您可以传递日志类型,如 .details、.fav 和 .social。
guard let engage = EngageSDK.shared else { return }
engage.callLogEvent(logType: .details, contentId: "contentId", contentType: "contentType", param2: nil, beacon: "current beacon", location: "current location") { (response) in
if let response = response {
print(response)
} else {
print("fail")
}
}
从SDK登出
从SDK登出会停止正在进行的扫描,并将所有设置重置为默认值。
engage.logout()