Xiaomi Mi Push

Follow this guide to get push notifications from Stream Chat using Xiaomi Mi Push.

Setting up Notifications on the Stream Dashboard

First, to get notifications from Stream, you’ll need to provide your Xiaomi app details on the Stream dashboard.

Go to the Xiaomi Console, and select the project your app belongs to.

Create a new Xiaomi project here if you don’t have one yet.

Click on App Info and locate the App ID and App secret, and copy them (if values are not shown, you need to click View button):

Locating your Xiaomi App

Locating your Xiaomi Credentials

Open the Stream Dashboard, and go to the Chat Overview page.

Navigating to the Chat Overview page on the Stream Dashboard

Find and select the Xiaomi switch. Paste your App ID and App secret, and click Save to confirm your changes.

Setting up your Xiaomi App ID and App Secret on the Stream Dashboard

That’s all you need to configure on the dashboard. You can now set up the client-side integration.

Receiving Notifications in the Client

First, add Xiaomi to your Android project. You need to download Xiaomi Mi Push SDK and add it to your project. At the time of writing this documentation, they don’t provide any Maven repository that you can use, so you need to download the.aar file manually and add it to the libs folder of your app, following their instructions.

dependencies {
    implementation files('libs/MiPush_SDK_Client_5_0_6-G_3rd.aar')
}

Stream Video for Android offers an artifact that allows easy setup of Xiaomi Mi Push. Add this dependency to your app’s build.gradle file:

dependencies {
    implementation "io.getstream:stream-android-push-xiaomi:$stream_version"
}

Then, create a XiaomiPushDeviceGenerator and add it to the list of generators in NotificationConfig, which you should pass into StreamVideoBuilder when you initialize the SDK:

val notificationConfig = NotificationConfig(
    pushDeviceGenerators = listOf(
        XiaomiPushDeviceGenerator(
            context = context,
            appId = "YOUR XIAOMI APP ID",
            appKey = "YOUR XIAOMI APP KEY",
            providerName = "xiaomi"
        )
    )
)
StreamVideoBuilder(
    context = context,
    user = user,
    token = token,
    apiKey = apiKey,
    notificationConfig = notificationConfig,
).build()

You must initialize StreamVideo before you can process push notifications. A good way to achieve this is by creating it within the Application class.

Your client is now set up to receive notifications from Stream using Xiaomi Mi Push.

Using a Custom PushMessageReceiver

The Stream Xiaomi push provider artifact contains a ChatXiaomiMessagingReceiver implementation that sends new Xiaomi tokens to Stream and forwards incoming push messages to StreamVideo to handle.

If you’re using Xiaomi notifications for other purposes inside your app as well, you will need your own custom receiver to replace this. Here, you have to call XiaomiMessagingDelegate’s registerXiaomiToken and handleMiPushMessage methods, like so:

class CustomPushMessageReceiver : PushMessageReceiver() {

    override fun onReceiveRegisterResult(context: Context, miPushCommandMessage: MiPushCommandMessage) {
        // Update device's token on Stream backend
        try {
            XiaomiMessagingDelegate.registerXiaomiToken(miPushCommandMessage, "xiaomi")
        } catch (exception: IllegalStateException) {
            // StreamVideo was not initialized
        }
    }

    override fun onReceivePassThroughMessage(context: Context, miPushMessage: MiPushMessage) {
        try {
            if (XiaomiMessagingDelegate.handleMiPushMessage(miPushMessage)) {
                // MiPushMessage was from Stream and it is already processed
            } else {
                // MiPushMessage wasn't sent from Stream and it needs to be handled by you
            }
        } catch (exception: IllegalStateException) {
            // StreamVideo was not initialized
        }
    }
}

Your custom receiver needs to have an <intent-filter> priority higher than -1 to replace our SDKs service. (By default, this priority is 0.)

Push Notification Payload

Push notifications are delivered as data payloads that the SDK can use to convert into the same data types that are received when working with the APIs.

When a call is started, Stream Server kicks a job that sends a regular data message (as below) to configured push providers on your app. When a device receives the payload, it’s passed to the SDK which connects to Stream Video Server to process the the call and show the notification to the final user.

This is the main payload which will be sent to each configured provider:

{
  "sender": "stream.video",
  "type": "call.ring | call.notification | call.live_started",
  "call_display_name": "Jc Miñarro",
  "call_cid": "default:77501ea4-0bd7-47d1-917a-e8dc7387b87f",
  "version": "v2",
}

© Getstream.io, Inc. All Rights Reserved.