🔔 Push notifications
Stay connected with your customers by utilizing push notifications to alert them of new chat messages, release notes, and news articles. This will deepen engagement and foster stronger relationships.
Gleap supports Firebase Cloud Messaging to send push notifications to users.
Setup Firebase Cloud Messaging
To make use of Gleap Push Notifications you must add the Firebase Cloud Messaging service to your app or website. Learn how to get started with FCM here.
Getting your Firebase Cloud Messaging Token
Open your project in Firebase and open the project settings.
Click on "Cloud Messaging" and, if it is not already enabled, activate the Firebase Cloud Messaging API (V1).
Proceed to the "Service Accounts" section and generate a new private key, which will automatically create a file named like serviceAccountKey.json
.
Next, open the project settings in Gleap, select "Push Notifications", and upload the serviceAccountKey.json
file. Finally, save your configuration.
Subscribe to the Gleap user topic
The last step to complete the push notification setup is to subscribe to the user topic, which Gleap will send the push notifications to. In order to do so, you will need to register the register & unregister push message topic callbacks.
// Learn more on:
// https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
import io.gleap.callbacks.RegisterPushMessageGroupCallback;
import io.gleap.callbacks.UnRegisterPushMessageGroupCallback;
import com.google.firebase.messaging;
Gleap.getInstance().setRegisterPushMessageGroupCallback(new RegisterPushMessageGroupCallback() {
@Override
public void invoke(String pushMessageGroup) {
FirebaseMessaging.getInstance().subscribeToTopic(pushMessageGroup)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// All done.
}
});
}
});
Gleap.getInstance().setUnRegisterPushMessageGroupCallback(new UnRegisterPushMessageGroupCallback() {
@Override
public void invoke(String pushMessageGroup) {
FirebaseMessaging.getInstance().unsubscribeFromTopic(pushMessageGroup)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// All done.
}
});
}
});
That's it - build and run your app 🚀
Handle push-notifications click
In order to open the message, the user clicked on, you will need to add some extra code to your app. The idea is to get the data from the push notification and then pass it to the SDK by utilizing handlePushNotification().
import android.os.Bundle
import org.json.JSONObject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
* Extension function to convert a Bundle to a JSONObject.
*/
fun Bundle?.toJsonObject(): JSONObject {
val jsonObject = JSONObject()
this?.let {
for (key in it.keySet()) {
val value = it.get(key)
jsonObject.put(key, value)
}
}
return jsonObject
}
// Usage within a CoroutineScope. You usually want to put this inside you onCreate function.
CoroutineScope(Dispatchers.Main).launch {
if (intent?.extras?.getString("sender") == "GLEAP") {
Gleap.getInstance().handlePushNotification(intent.extras.toJsonObject())
}
}