Push Notifications

Push Notifications provide a way to deliver some information to user while he is not using your app actively. The following use cases can be covered additionally with push notifications:

  • send a chat message if recipient is offline (a push notification will be send automatically)
  • make a video call with offline opponents (need to send a push notification manually)
  • request to add a user to contact list (need to send a push notification manually)

Configure Firebase project and Server key

In order to start working with push notifications functionality you need to configure it.

  1. Create and configure your Firebase project and obtain the Server key. If you have any difficulties with Firebase project registration, follow our guide.

    To find your FCM server key go to your Firebase console >> Cloud Messaging section: Find your FCM server key

  2. Copy your FCM server key to your Dashboard >> Your App >> Push Notifications >> Certificates & API keys, select the environment for which you are adding the key and hit Save key. Use the same key for development and production zones. Add your FCM server key to your Dashboard

  3. If you have not added Firebase to your project previously, follow this guide to Connect Firebase SDK.

Prepare app dependencies

1. As part of enabling Firebase services in your Android application you need to add the google-services plugin to your Project build.gradle file:

dependencies {
    classpath 'com.google.gms:google-services:3.0.0'

Add FCM dependency

If you use google play services in your project, use version 11.8.0 or higher (version we use in our SDK) as all com.google.android.gms libraries must use the exact same version specification.

If you use version higher than 11.8.0, then just add explicit dependency:

compile "com.google.android.gms:play-services-gcm:17.0.0"
compile "com.google.firebase:firebase-messaging:17.0.0"
compile "com.google.firebase:firebase-core:17.0.0"

Add FCM dependency

2. Include gms plugin in the bottom of your module build.gradle :

apply plugin: 'com.google.gms.google-services'

Include gms plugin

3. For GCM push type add an empty current_key settings into your google-services.json file for your package name (this paragraph relates to GCM. If you use FCM and already have some value in current_key field, leave it as is):

"client": [
      "client_info": {
        "mobilesdk_app_id": "1:861750218637:android:c7299bc46191b2d7",
        "client_id": "android:com.your.app.package.id",
        "client_type": 1,
        "android_client_info": {
          "package_name": "com.your.app.package.id"
      "api_key": [
          "current_key": ""

Add current_key

Prepare app AndroidManifest

  1. Copy Sender ID value from your Firebase console:

    Find your Sender ID

  2. Edit your app AndroidManifest.xml file and add your Firebase Sender ID as well as notification type and environment there:

To integrate automatic push subscription you just need set values in AndroidManifest:

<meta-data android:name="com.connectycube.pushnotifications.TYPE" android:value="FCM" />
<meta-data android:name="com.connectycube.pushnotifications.SENDER_ID" android:value="@string/sender_id" />
<meta-data android:name="com.connectycube.pushnotifications.ENVIRONMENT" android:value="DEVELOPMENT" />
  • com.connectycube.pushnotifications.TYPE - can be FCM
  • com.connectycube.pushnotifications.SENDER_ID - your sender id from google console (for ex.8617520217632)
  • com.connectycube.pushnotifications.ENVIRONMENT - can be DEVELOPMENT or PRODUCTION

Add your Sender ID, notification type and environment

Then you need to setup FcmPushListenerService and FcmPushInstanceIDService:

       <action android:name="com.google.firebase.MESSAGING_EVENT" />
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

Automatic push subscription

ConnectyCube Android SDK provides automatic push subscription management. It means you don’t need bother how to get FCM device token, create push subscription and what to do with the received data. Thus, you can reduce your code and make it cleaner.

Here are the requirements on how to integrate automatic push subscription feature to your app:

Enable/Disable push subscription

Here you can use global setting to enable or disable delivery of push notifications (means to set this parameter only one time):

ConnectycubeSettings.getInstance().setEnablePushNotification(false); // default is 'true'

boolean isEnabled = ConnectycubeSettings.getInstance().isEnablePushNotification();
ConnectycubeSettings.getInstance().isEnablePushNotification = false // default is 'true'

val isEnabled = ConnectycubeSettings.getInstance().isEnablePushNotification

Track subscription status

To be aware about what is happening with your push subscription, whether you're subscribed successfully or not, you can use the SubscribeListener. Just add SubscribeListener right after the ConnectycubeSettings.getInstance().init() code:

ConnectycubePushManager.getInstance().addListener(new ConnectycubePushManager.SubscribeListener() {
    public void onSubscriptionCreated() {


    public void onSubscriptionError(final Exception e, int resultCode) {
        Log.d(TAG, "onSubscriptionError" + e);
        if (resultCode >= 0) {
            Log.d(TAG, "Google play service exception" + resultCode);

    public void onSubscriptionDeleted(boolean success) {

    .addListener(object : ConnectycubePushManager.SubscribeListener {
        override fun onSubscriptionCreated() {}

        override fun onSubscriptionError(e: Exception, resultCode: Int) {
            Log.d(TAG, "onSubscriptionError$e")

            if (resultCode >= 0) {
                Log.d(TAG, "Google play service exception$resultCode")

        override fun onSubscriptionDeleted(success: Boolean) {


Manual push subscription

If you don’t want to use automatic push subscription feature, then do the following:

  • Set SubscribePushStrategy.MANUAL as the main strategy:
// default SubscribePushStrategy.ALWAYS
// default SubscribePushStrategy.ALWAYS
ConnectycubeSettings.getInstance().subscribePushStrategy = SubscribePushStrategy.MANUAL

In this case you need to subscribe and unsubscribe manually using the following methods:

boolean tokenRefreshed = false;
SubscribeService.subscribeToPushes(context, tokenRefreshed);

// this comes from 'InstanceIDListenerService'
public void onTokenRefresh() {
    boolean tokenRefreshed = true;
    SubscribeService.subscribeToPushes(context, tokenRefreshed);
val tokenRefreshed = false
SubscribeService.subscribeToPushes(context, tokenRefreshed)

// this comes from 'InstanceIDListenerService'
override fun onTokenRefresh() {
    val tokenRefreshed = true
    SubscribeService.subscribeToPushes(context, tokenRefreshed)

Send push notifications

You can manually initiate sending of push notifications to user/users on any event in your application. To do so you need to form a push notification parameters (payload) and set the push recipients:

ArrayList<Integer> recipientsUserIds = new StringifyArrayList<Integer>();

ConnectycubeEvent event = new ConnectycubeEvent();

JSONObject json = new JSONObject();
try {
    json.put("message", "New bitcoin trends today");
    // custom parameters
    json.put("user_id", "56");
    json.put("dialog_id", "54123123123416234614263123");
} catch (Exception e) {


ConnectycubePushNotifications.createEvent(event).performAsync(new EntityCallback<ConnectycubeEvent>() {
    public void onSuccess(ConnectycubeEvent event, Bundle args) {
        // sent

    public void onError(ResponseException errors) {

val userIds = StringifyArrayList<Int>().apply {

val event = ConnectycubeEvent().apply {
    this.userIds = userIds
    environment = ConnectycubeEnvironment.DEVELOPMENT
    notificationType = ConnectycubeNotificationType.PUSH

val json = JSONObject()
json.put("message", "New bitcoin trends today")

// custom parameters
json.put("user_id", "56")
json.put("dialog_id", "54123123123416234614263123")

event.message = json.toString()

    .performAsync(object : EntityCallback<ConnectycubeEvent> {
        override fun onSuccess(event: ConnectycubeEvent, args: Bundle?) {
            // sent

        override fun onError(errors: ResponseException) {


Receive push notifications

To receive push notifications you need to register the BroadcastReceiver only:

private BroadcastReceiver pushBroadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
        String from = intent.getStringExtra("from");
        Log.i(TAG, "New push message: " + message + ", from " + from);

    new IntentFilter("new-push-event"));
val pushBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val message = intent.getStringExtra("message")
        val from = intent.getStringExtra("from")
        Log.i(TAG, "New push message: $message, from $from")