iOS Sinch Push SDK
This guide demonstrates how to easily implement our SDK for push features.
Prerequisites
- Xcode 14+ and a familiarity with how to use the Xcode UI.
- Swift Package Manager (iOS 13+)
- Access to SDK GitHub repository.
Important!
This SDK GitHub repository is private so you need to email us to gain access.
Installing the dependencies
You can install the necessary dependencies using either the Swift package manager inside of Xcode.
Swift Package Manager:
To install the dependency using the Swift package manager:
-
In Xcode, navigate to the Swift package manager interface and enter the following link
https://github.com/sinchlabs/sinch-chat-ios-sdk
. - Log in to your GitHub account using your GitHub credentials and your GitHub personal token .
Configuring the application
To successfully set up the Sinch Chat client to get it ready for sending and receiving messages, you must complete each of the following steps:
- Create a new project in Xcode.
-
Configure
permissions in the
info.plist
file. - Initialize the SDK as soon as the application starts.
-
Set the identity
as soon as you can authenticate the user using your internal
UserID
and sign it using a cryptographic algorithm on the backend server side. (You can sign it on mobile but it is NOT RECOMMENDED ) ← this method can be called multiple times. - Set up push notifications.
Info.plist permissions
We need three permissions in info.plist
:
- Push notifications permission and microphone access:
-
Capability →
Push Notifications
-
Capability →
background modes
→Remote notifications
+ explanation in info.plist:
Verify this code is in 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>
Initializing the SDK
Open the AppDelegate
file and replace all of the default content with the following code:
import UIKit
import SinchChatSDK
let projectID = "your_Project_Id"
let clientID = "your_Client_Id"
let secret = "your_secret"
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
SinchChatSDK.shared.initialize()
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
SinchChatSDK.shared.push.sendDeviceToken(deviceToken)
}
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([])
}
}
This code imports the Sinch Chat SDK for use and initializes it as soon as the application starts.
Set the configuration parameters
To connect your Sinch Chat client to your Sinch account, you need to set the following parameters in the AppDelegate
file:
Parameter | Description |
---|---|
projectID |
Get your project ID from your Sinch Dashboard. |
clientID |
Get your client ID from your Sinch Dashboard. |
secret |
If you have configured an optional secret token in your Sinch Dashboard, enter it here. |
region |
Use either EU1 for Europe or US1 for the United States. |
configID |
Get your configuration ID from your Sinch Dashboard |
Set identity
Next, we need to set the identity of the user in the application. Open the ViewController
file and replace the existing content with the following code:
import UIKit
import SinchChatSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
SinchChatSDK.shared.setIdentity(with: .init(clientID: clientID, projectID: projectID, configID: configID, region: .EU1), identity: .anonymous) { result in
switch result {
case .success:
DispatchQueue.main.async {
do {
let chatViewController = try SinchChatSDK.shared.chat.getChatViewController()
chatViewController.modalPresentationStyle = .fullScreen
self.present(chatViewController, animated: true, completion: nil)
} catch {
// all errors are related with bad configuration. You can check if chat is available using `isChatAvailable` method.
}
}
case .failure(let error): break
// SDK cannot be initizalized so you can't use any functionality :/
}
}
}
}
Additionally, you need to set the region of the app you are using. You can use .EU1
for Europe or .US1
for the United States.
Setting up push notifications
APNS certificate
To use push notifications with this SDK you have to provide an APNS Key and upload it to the Sinch Dashboard.
Note:
Push notifications only work on actual mobile devices. You cannot test push notifications using the app emulator inside of Xcode.
Xcode Capabilities
Make sure you’ve enabled the Remote notifications
capability in Background Modes
in your target configuration in the Signing & Capabilities
tab and the Push Notifications
tab.
Methods in AppDelegate.swift or AppDelegate.m:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
SinchChatSDK.shared.push.sendDeviceToken(deviceToken)
}
Note:
Make sure you’ve added UNUserNotificationCenterDelegate
to the AppDelegate
definition or to your own class.
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([])
}
Additional methods
Those methods are optional and they are only used if you want to ask for notifications in other places than after starting the chat. You shouldn't ask the user for notification at application start. It is a bad practice. Ask when you need it.
Possibility to check push permission using our method:
SinchChatSDK.shared.push.checkPushPermission(completion: @escaping (SinchSDKNotificationStatus) -> Void)
// OR iOS 13+ with Combine
func checkPushPermission() -> AnyPublisher<SinchSDKNotificationStatus, Never>
Possibility to ask for notifications permission:
func askNotificationPermission(completion: @escaping (SinchSDKNotificationStatus) -> Void)
// OR iOS 13+ with Combine
func askNotificationPermission() -> AnyPublisher<SinchSDKNotificationStatus, Never>
Next steps
After your client is set up, now you can configure the Sinch Push channel in the Conversation API and start sending and receiving messages.