Skip to content

Instantly share code, notes, and snippets.

@lfmundim
Created October 29, 2018 23:07
Show Gist options
  • Save lfmundim/b46360605464b2853dd1a00143170410 to your computer and use it in GitHub Desktop.
Save lfmundim/b46360605464b2853dd1a00143170410 to your computer and use it in GitHub Desktop.
Integrating an BLiP Chat enabled Android App with Firebase Cloud Messaging

On Firebase

  1. Create a Firebase account

  2. Go to your Console and create a new project

  3. Open your newly created project and click on Grow->Cloud Messaging

  4. Click on the Android Logo in the top banner and follow the steps indicated there. They are:

    1. Write your Android Package Name in the required field. Your package name is generally the applicationId in your app-level build.gradle file
    2. Download the generated google-services.json
    3. Add Firebase to your Android project. In Android Studio:
    • in the project-level .gradle file:
    buildscript {
        dependencies {
            // Add this line
            classpath 'com.google.gms:google-services:4.0.1'
        }
    }
    • in the app-level .gradle file:
    dependencies {
    // Add this line
        implementation 'com.google.firebase:firebase-core:16.0.1'
    }
    ...
    // Add to the bottom of the file
    apply plugin: 'com.google.gms.google-services'
    • resync your gradle
    • reinstall your app if needed to make it communicate with Firebase

Once it is communicating, you can test a push notification by using this HTTP Request:

POST https://fcm.googleapis.com/fcm/send HTTP/1.1
content-type: application/json; charset=UTF-8
Authorization: key=<server_key>
Host: fcm.googleapis.com

{
    "notification": {
        "title": "Firebase",
        "body": "Firebase is awesome"
    }
}

The server_key can be found in your Project's Settings->Cloud Messaging->Project Credentials->Server key

Inside the App

Using Kotlin/Java

  1. Wherever you choose to open the BLiP Chat window, alongside with other authentication info you are providing, you need to provide the following extra key: "#inbox.forwardTo":"<firebaseCloudMessagingToken>@firebase.gw.msging.net". You can do so with the following code:
    // Firebase
    val extras = HashMap<String, String>()
    val fcmUserToken = FirebaseInstanceId.getInstance().token
    extras.put("#inbox.forwardTo", String.format("%s@firebase.gw.msging.net", fcmUserToken))
    val identity = "UniqueUserId"

    // BLiP
    val authConfig = AuthConfig(AuthType.Dev, identity,"pass123PS")
    val account = Account()
    account.setFullName("TEST SUBJECT")
    account.setEmail("email@domain.com")

    val blipOptions = BlipOptions()
    blipOptions.setAuthConfig(authConfig)
    blipOptions.setAccount(account)
    account.setExtras(extras);
  1. You can test single user notifications with the token you get above, by adding a to parameter to the previous call:
POST https://fcm.googleapis.com/fcm/send HTTP/1.1
content-type: application/json; charset=UTF-8
Authorization: key=<server_key>
Host: fcm.googleapis.com

{
    "notification": {
        "title": "Firebase",
        "body": "Firebase is awesome"
    },
    "to": "<fcmUserToken>"
}

BLiP

After doing the above steps, you need to e-mail integracoes@blip.ai with the subject "Integração FCM". A BLiP representative will reach you as soon as possible asking for the google-services.json to manually add the integration with the platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment