[OLD v54] Setting up your project to work with the SDK - GlobalMessageServices/BCS-GMS-SDK-Android GitHub Wiki

Table of contents

  1. Add Firebase cloud messaging to your project
  2. Get credentials for your app
  3. Add the SDK to your project
  4. Extend the PushKFirebaseService
  5. Start using the SDK
  6. More

Add Firebase cloud messaging to your project

You need to connect your app to your firebase project

image

  • Add required dependencies to your android studio project (either manually or use Firebase Assistant plugin for Android Studio)

image

image

image

Recommended dependency versions to use within your app (or use whatever would be compatible with what is used within the SDK):

  • Project (top-level) build.gradle:
buildscript {
    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.4'
    }
}
  • Module (app-level) build.gradle:
plugins {
    ...
    id 'com.google.gms.google-services'
}

dependencies {
    ...
    implementation 'com.google.firebase:firebase-messaging:21.0.0'
}
  • Make sure you add proper google-services.json file to the root of your app module

Read more at: https://firebase.google.com/docs/android/learn-more

Get credentials for your app

  • Provide your firebase project's cloud messaging server key to the PushSDK representative, and obtain required credentials to work with the SDK

image

image

Add the SDK to your project

Make sure you have declared maven repository in your project (top-level) build.gradle

allprojects {
    repositories {
        ...
        maven {
            url 'https://jitpack.io'
        }
    }
}

Add dependency to your module (app-level) build.gradle

dependencies {
    ...
    //or use a newer version if available
    implementation 'com.github.GlobalMessageServices:Hyber-GMS-SDK-Android:v1.0.0.54-RELEASE'
}

v1.0.0.50 and higher now supports http protocol

To use http protocol instead of https, add android:usesCleartextTraffic="true" to your application tag inside android manifest

<application
        ...

        android:usesCleartextTraffic="true"
        >

Extend the PushKFirebaseService

Create a class that extends PushKFirebaseService

Specify title and text which may be displayed by the system in the "summary notification".

Read more about receiving push messages and displaying notifications using the PushSDK here

class MyPushKFirebaseService : PushKFirebaseService(
    summaryNotificationTitleAndText = Pair("title", "text")
) {
    override fun setNotificationStyle(
        notificationConstruct: NotificationCompat.Builder,
        data: Map<String, String>,
        notificationStyle: NotificationStyle
    ) {
        super.setNotificationStyle(notificationConstruct, data, notificationStyle)
    }

    override fun onReceiveDataPush(appIsInForeground: Boolean, remoteMessage: RemoteMessage) {
        super.onReceiveDataPush(appIsInForeground, remoteMessage)
    }

    override fun onDisplayNotification(appIsInForeground: Boolean, remoteMessage: RemoteMessage) {
        super.onDisplayNotification(appIsInForeground, remoteMessage)
    }

    override fun onUnableToDisplayNotification(
        areNotificationsEnabled: Boolean,
        appIsInForeground: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onUnableToDisplayNotification(
            areNotificationsEnabled,
            appIsInForeground,
            remoteMessage
        )
    }
}

Add the service to your AndroidManifest.xml

<application
...>

<service
    android:name=".MyPushKFirebaseService"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

</application>

Start using the SDK

Make sure you have extended the PushKFirebaseService and added it to the AndroidManifest.xml

Init the SDK with the following basic parameters:

  • context - the context you would like to use
  • baseApiUrl - base URL path to the API
val pushSDK = PushSDK(
    context = this,
    baseApiUrl = "https://yourapilink.com/version" //e.g. https://yourapilink.com/3.0
)

Register your device/application:

  • Either register your device by using:
val answer1 = pushSDK.registerNewDevice(
    "clientAPIKey",  //API key that you would be provided with
    "appFingerprint", //APP fingerprint that you would be provided with
    "88002000600", //Device's phone number
    "UserPassword" //password, associated with Device's phone number (legacy - it is unused, you can put any value)
)
Log.d("TAG", answer1.toString())

Which would produce a similar output if successful:

D/TAG: PushKFunAnswerRegister(code=200, result=Ok, description=Success, deviceId=1066, token=c71b4bc05ee24f8eac007cc63d8ff2c4, userId=82, userPhone=88002000600, createdAt=2020-10-29T08:12:33.194942+00)

Then update registration:

val answer2 = pushSDK.updateRegistration()
Log.d("TAG", answer2.toString())

Which would produce a similar output if successful:

D/TAG: PushKFunAnswerGeneral(code=200, result=OK, description=Success, body={"deviceId": 1066})

This will register the device within the system and automatically pick up your current firebase cloud messaging token

  • Or register your device by manually specifying your firebase cloud messaging token:
val answer1 = pushSDK.registerNewDevice(
    "clientAPIKey",  //API key that you would be provided with
    "appFingerprint", //APP fingerprint that you would be provided with
    "userMsisdn", //Device's phone number
    "userPassword", //password, associated with Device's phone number (legacy - it is unused, you can put any value)
    "firebaseToken" //your FCM registration token (Optional - you can call the method without specifying it)
)
Log.d("TAG", answer1.toString())

This will register the device within the system and use the specified firebase cloud messaging token

Note: In case you are trying out this code for the first time, you may want to unregister all devices before registering a new one, so that registration would pass every time. You may do so by adding the following code before registering a device:

val answer0 = pushSDK.unregisterAllDevices()
Log.d("TAG", answer0.toString())

Your application should now be able to receive push messages from the api

Note: By default, notifications would only appear when your application is in background.

For more usage information - please see:

⚠️ **GitHub.com Fallback** ⚠️