SinchChatSDK 0.11.14

SinchChatSDK 0.11.14

Piotr Michalik 维护。



 
依赖项
gRPC-Swift~> 1.8.0
Kingfisher~> 7.10.0
 

SinchChatSDK 0.11.14

  • Sinch

iOS Sinch SDK - 入门

这是如何轻松实现我们的 SDK 用于聊天和推送功能的文档。

要求

  • Xcode 14+

  • Swift 包管理器(需 iOS 13+)

安装

Swift 包管理器

  1. 复制到本仓库的链接 https://github.com/sinchlabs/sinch-chat-ios-sdk

  2. 粘贴到 xCode SPM 并使用 GitHub 个人令牌登录您的 GitHub 账户。

第一步

  1. 在应用程序启动时立即初始化 SDK。

  2. 您可以使用您的内部 UserID 尽快认证用户,并在后端静态算法上签名(您可以在移动设备上签名,但不建议)→ 这种方法可以被调用多次。

  3. 设置推送通知。

  4. 显示聊天内容。

Info.plist 权限

我们需要在 info.plist 中添加两个权限

  • 推送通知权限和麦克风访问

  • 功能 → 推送通知

  • 功能 → 后台模式远程通知 + 在 info.plist 中的说明

<dict>

<key>UIBackgroundModes</key>

<array>

<string>remote-notification</string>

</array>

<key>NSMicrophoneUsageDescription</key>

<string>Need microphone access for sending voice messages</string>

</dict>

  

初始化

SinchChatSDK.shared.initialize()

尽可能早地打开意味着最好的地方是在 AppDelegate 中的

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

示例

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

SinchChatSDK.shared.initialize()

return true

}

  

配置

我们需要为您提供的聊天服务进行配置,这意味着您需要

  • 区域

  • 项目 ID

  • 客户端 ID

  • 配置 ID(如果没有,请留空)

  • *(可选)令牌密钥

设置身份

第二步是授权用户,因此我们需要的您在尽快授权用户后调用此方法。您可以随时多次调用此方法

  

func setIdentity(with config: SinchSDKConfig.AppConfig, identity: SinchSDKIdentity, completion: ((Result<Void, SinchSDKIdentityError>) -> Void)? = nil)

  

示例

let config = SinchSDKConfig.AppConfig(
        clientID: {{ client_id }}, 
        projectID: {{ project_id }}, 
        configID: {{ config_id }}, // If you don't have it, leave empty string "" 
        region: .EU1
)
  

SinchChatSDK.shared.setIdentity(with: config, identity: currentIdentityType) { result in

switch result {

case .success:

// SDK is authenticated successfully.

case .failure(let error):

// SDK cannot be initizalized so you can't use any functionality :/

}

}

自定义用户 ID 签名

如果为匿名会话,我们将生成唯一的用户标识符,以在您的项目 ID 和客户端 ID 范围内有唯一用户标识符。

如果为已签名的用户 ID,您可以指定用户 ID,我们将在项目 ID、客户端 ID 和配置 ID 的范围内使用它。这是什么意思?您可以发送推送通知或启动直播聊天,标识符分配在您的数据库中。

示例

  • 匿名会话 我们正在生成随机用户ID,因此它将是这样:01GYC7B5TR5QR1NE2NRQDPGP0X。如果您想开始聊天或发送推送通知,您需要将我们的用户ID保存到您的数据库中,并在我们系统中使用它来进行一些交互。
  • 已签入会话 在您的数据库/系统中,您将该用户以标识符543234或电话号码2124567890保存。您可以使用543234标识符或电话号码登录我们的SDK。您可以用标识符或电话号码发送推送/开始聊天,而不是使用我们的标识符。

如何使用您的标识符登录?

示例

import SinchChatSDK
//...
let userID = "{your_unique_id}"
let signedUserID: String = userID.hmac(algorithm: CryptoAlgorithm.SHA512, key: {your_secret})

SinchChatSDK.shared.setIdentity(with: {{config}}, identity: .selfSigned(userId: userID, secret: signedUserID))

现在您可以使用SDK的功能,如聊天或推送。

聊天

检查聊天可用性

要检查聊天是否对用户可用,您应该使用此方法

public enum SinchSDKChatAvailability {

/// The Chat is ready to user.

case available

/// The Chat is not initialized

/// Tip: use initialize method to initialize chat.

case uninitialized

  

/// The chat is not authorized

/// Tip: User setIdentity method to authorize the client.

case authorizationNeeded

  

/// The chat is opened

/// You cannot run two chats at once.

case currentlyRunning

}

  

func isChatAvailable() -> SinchSDKChatAvailability

  

显示聊天

在显示聊天之前,请确保您已初始化此SDK。

您有几个选项可以显示聊天。

您可以使用自己的 UINavigationController 来显示我们的聊天。

您可以直接使用标准的过渡效果或自定义的过渡效果来显示我们的聊天。

为此,在 SinchChatSDK.shared.chat.* 中有一个方法。

💡 如果您传递 navigationController == nil,那么我们将创建自己的导航控制器,因为无论如何我们都需要它。

func getChatViewController(navigationController: UINavigationController?,

uiConfig: SinchSDKConfig.UIConfig?,

localizationConfig: SinchSDKConfig.LocalizationConfig?) throws -> UIViewController

💡 我们正在返回 UIViewController,您必须用您自己的方式显示它。

💡 目前我们只支持英语语言,如果您还想支持其他语言,请重写 localizationConfig 参数。

示例

do {

let chatViewController = try SinchChatSDK.shared.chat.getChatViewController()

chatViewController.modalPresentationStyle = .fullScreen

present(chatViewController, animated: true, completion: nil)

} catch {

// all errors are related with bad configuration. You can check if chat is available using `isChatAvailable` method.

}

推送

APNS证书

要使用此SDK的推送通知功能,您需要提供APNS密钥并将其上传到我们的Sinch面板。

💡 推送通知仅在真实设备上工作。

Xcode能力

请确保您在目标配置的签名与能力选项卡中,在后台模式中勾选了移除通知能力,并在推送通知中勾选了。

AppDelegate.swift或AppDelegate.m中的方法

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

SinchChatSDK.shared.push.sendDeviceToken(deviceToken)

}

  

💡 确保您已经将UNUserNotificationCenterDelegate添加到AppDelegate定义中或您的自定义类中。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

  

if SinchChatSDK.shared.push.handleNotificationResponse(response) {

// this is our notification and it is handled by SinchChatSDK

}

  

completionHandler()

}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

  

if let presentationOptions = SinchChatSDK.shared.push.handleWillPresentNotification(notification) {

completionHandler(presentationOptions)

return

}

completionHandler([])

}

  

附加方法

这些方法是可选的,如果您想在开始聊天之后在其他地方请求通知,那么它们只是您的附加工具。请不要在应用程序启动时请求用户的提醒。这不是一个最佳实践。在需要时再请求。

使用我们的方法检查推送权限的可能性

SinchChatSDK.shared.push.checkPushPermission(completion: @escaping (SinchSDKNotificationStatus) -> Void)

  

// OR iOS 13+ with Combine

func checkPushPermission() -> AnyPublisher<SinchSDKNotificationStatus, Never>

请求通知权限的可能性

func askNotificationPermission(completion: @escaping (SinchSDKNotificationStatus) -> Void)

  

// OR iOS 13+ with Combine

func askNotificationPermission() -> AnyPublisher<SinchSDKNotificationStatus, Never>