Skip to content
Last updated

Set up push notifications.

Firebase token

To use push notifications with this SDK, you have to provide Firebase token and upload it to our Sinch Panel.

Note:

Push notification works via Firebase service, so you need to make sure we have your server token.

Initialization SDK with push notification token

Below is an example of intializing the SDK with a push notification token:

FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->

    val options = SinchInitializationOptions(null)
    if (task.isSuccessful) {
        options.pushDeviceToken = task.result
    }
    SinchChatSDK.initialize(applicationContext, options)
}

Set up Firebase Messaging service

To set up the Firebase Messsaging service:

  1. Add these dependencies to your app using the build.gradle file:

    implementation platform('com.google.firebase:firebase-bom:{version}')
    implementation 'com.google.firebase:firebase-messaging:{version}'
    implementation 'com.google.firebase:firebase-analytics-ktx'
  2. The AndroidManifest.xml file adds a new service for push notifications;

    <service
        android:name=".AppFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  3. Create file called AppFirebaseMessagingService to match android:name. Copy the code below and paste it into the file:

    class AppFirebaseMessagingService : FirebaseMessagingService() {
    
        override fun onNewToken(p0: String) {
            super.onNewToken(p0)
            SinchChatSDK.push.onNewToken(p0)
        }
    
        override fun onMessageReceived(p0: RemoteMessage) {
            super.onMessageReceived(p0)
            if (SinchChatSDK.push.onMessageReceived(p0)) {
                // This notification belongs to SinchChatSDK.
                return
            }
        }
    }

Start conversation when starting the Chat.

If you want to initialize a conversation for your user, you must start the chat with the following option shown in the example below:

val options = SinchStartChatOptions(shouldInitializeConversation = true)
SinchChatSDK.chat.start(this, options)

Troubleshooting

You may encounter issues with building your application. These may be related to Protobuf dependencies.

To troubleshoot, paste these lines in the android {} section in build.gradle:

android {
    ...

    configurations {
        implementation.exclude module:'protolite-well-known-types'
        implementation.exclude module:'protobuf-lite'
    }
}