Suggestions

close search

Push notification

Push notifications allow your app to receive chat events while it is in the background or not running. Stringee uses Firebase Cloud Messaging (FCM) on Android.

1. Configure Firebase Cloud Messaging

Add your Android app to a Firebase project, download google-services.json to the app/ directory, and configure the Google Services Gradle plugin. Add Firebase Messaging by using the Firebase BoM:

dependencies {
    implementation platform('com.google.firebase:firebase-bom:34.17.0')
    implementation 'com.google.firebase:firebase-messaging'
}

2. Register the FCM token

After StringeeClient has connected, get the FCM token and register it with Stringee:

FirebaseMessaging.getInstance().getToken().addOnSuccessListener(deviceToken -> {
    stringeeClient.registerPushToken(deviceToken, new StatusListener() {
        @Override
        public void onSuccess() {
        }

        @Override
        public void onError(StringeeError error) {
        }
    });
});

Register a refreshed token in your FirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String token) {
        if (stringeeClient != null && stringeeClient.isConnected()) {
            stringeeClient.registerPushToken(token, new StatusListener() {
                @Override
                public void onSuccess() {
                }
            });
        }
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        if ("CHAT_EVENT".equals(data.get("type"))) {
            // Parse data.get("data") and show your app notification.
        }
    }
}

Declare the service in AndroidManifest.xml:

<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

On Android 13 or later, declare POST_NOTIFICATIONS and request it at runtime before displaying notifications:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

3. Unregister the token

When the user signs out, unregister the token before disconnecting the client:

stringeeClient.unregisterPushToken(deviceToken, new StatusListener() {
    @Override
    public void onSuccess() {
    }
});