Inngage iOS
安装
要安装库,请使用 CocoaPods。在 Podfile
文件中为主项目添加以下行
pod 'Inngage/Core'
对于 targets 扩展项目,如与远程通知代码操作相关的 Notification Service Extension
和 Notification Content Extension
,请添加以下行
pod 'Inngage/NotificationExtension'
项目分为 2 个模块,Core
模块具有应用打开时管理 push notification 的作用域,而 NotificationExtension
模块则管理通知箱中的 push notification 以及 rich push notification。
仍然在 Podfile
文件中,添加以下 post script
,以禁用运行 extension 框架编译时非 API 的验证
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
end
end
end
然后只需在终端中运行该项目路径的 pod install
命令。从现在起,将使用具有 .xcworkspace
扩展名的文件。
在
Example
项目中可以查看Podfile
如何编写和分配了所述代码。
配置
请查阅
README_ObjectiveC.md
文件了解使用 Objective-C 项目进行配置的信息。
项目
需要配置项目以便包含位于根目录下 Xcode 内 .xcproject
文件中的 Capabilities
。在 Xcode
的 Signing & Capabilities
选项卡中找到它。启用 Push notification
和 Background modes
时,会启用 Background fetch
和 Remote notifications
属性。
处理通知
在项目中,在 AppDelegate
文件中需要对 SDK 进行一些配置,以便与 Inngage 平台同步。
需要导入 SDK:import Inngage
包括以下类变量。
var pushNotificationManager = PushNotificationManager.sharedInstance()
var userInfoDictionary: [String: Any]?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
pushNotificationManager?.inngageAppToken = "APP_TOKEN"
pushNotificationManager?.inngageApiEndpoint = "https://api.inngage.com.br/v1"
pushNotificationManager?.defineLogs = true
pushNotificationManager?.enabledShowAlertWithUrl = false
pushNotificationManager?.enabledAlert = false
if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any] {
self.userInfoDictionary = userInfo
}
return true
}
需要插入 Inngage 控制台中提供的 Token,以导向您的应用程序的调用。一些变量可配置 SDK。
defineLogs
(布尔型):允许在 Xcode 控制台中查看日志。enabledAlert
(布尔型):当推送通知发送时,如果用户打开通知,且应用程序处于活动模式,则会显示系统标准警报,包括通知标题和发送的通知文本。enabledShowAlertWithUrl
(布尔型):当推送通知包含 URL 时,如果将其定义为false
,则不会显示警报。此属性在enabledAlert
设置为true
时起作用。
此部分将实现用户在推送通知 API 上的注册,包括设备的详细信息,以提供新的推送。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let userInfo = ["name": "XXX"]
pushNotificationManager?.handlePushRegistration(deviceToken, identifier: "USER_IDENTIFIER", customField: userInfo)
if let userInfoDictionary = userInfoDictionary {
pushNotificationManager?.handlePushReceived(userInfoDictionary, messageAlert: true)
}
}
此部分将记录推送通知注册中的失败情况。
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
pushNotificationManager?.handlePushRegistrationFailure(error)
}
此部分代码将向 Inngage 服务器的接收推送发送日志。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
pushNotificationManager?.handlePushReceived(userInfo, messageAlert: true)
}
通知服务扩展
在项目中,需要通过 Xcode 在根级别 .xcodeproj
文件中配置一个新扩展。这样就可以添加 Notification Service Extension
扩展。
将生成一个新文件序列。如果项目使用 Swift 语言,则需要在以下方法中添加以下代码
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
NotificationManager.prepareNotification(with: request, andBestAttempt: bestAttemptContent) { [weak self] (bestAttemptContent) in
if let bestAttemptContent = bestAttemptContent {
self?.contentHandler?(bestAttemptContent)
}
}
}
需要导入以下类:
import Inngage
。
此代码段将实现当通知到达最终用户时对远程通知的配置。
通知内容扩展
在项目中,需要通过 Xcode 在根级别 .xcodeproj
文件中配置一个新扩展。这样就可以添加 Notification Content Extension
扩展。
将生成一个新文件序列。如果项目使用 Swift 语言,则需要在以下方法中添加以下代码
func didReceive(_ notification: UNNotification) {
NotificationManager.prepareNotificationContent(with: notification, andViewController: self)
}
需要导入以下类:
import Inngage
。
在没有 Info.plist
文件时,需要在以下类别中添加一个新的属性:NSExtension
-> NSExtensionAttributes
,使用以下键 UNNotificationExtensionUserInteractionEnabled
和值 YES
。这个属性将允许丰富的推送通知能够进行用户交互。
下面的代码段将配置富推送通知,当用户通过强触屏或长按系统通知栏时进行交互。这样可以为用户提供图片(jpg、png、gif)和视频(mp4)。