Posted on

Android java app with notification from mvc using firebase cloud messaging – part 1

Firebase Project

For backend developers.

Follow the document :

Important steps : Add Firebase using the Firebase console. After you have a Firebase project, you can add your Android app to it. Enter your app’s package name in the Android package name field. Click Download google-services.json to obtain your Firebase Android config file google-services.json)

Android app in java

For frontend developers

  • Install or update Android Studio to its latest version.
  • Make sure that your app meets the following requirements:
    • Targets API level 16 (Jelly Bean) or later
    • Uses Gradle 4.1 or later

In Android Studio create a new android app using java as language. To enable Firebase products in your app, pass to Project visualization (on the left of android studio) in your root-level Gradle file (build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google’s Maven repository, as well.

buildscript {

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository

  dependencies {
    // ...

    // Add the following line:
    classpath ''  // Google Services plugin

allprojects {
  // ...

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    // ...

In your module (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin:

apply plugin: ''
// Add the following line:
apply plugin: ''  // Google Services plugin

android {
  // ...

To your module (app-level) Gradle file (usually app/build.gradle), add the dependencies for the Firebase products that you want to use in your app.

dependencies {
  // ...

  // Add the SDKs for the Firebase products you want to use in your app
  // For example, to use Firebase Authentication and Cloud Firestore
  implementation ''
  implementation ''

  // Getting a "Could not find" error? Make sure that you've added
  // Google's Maven repository to your root-level build.gradle file

Sync your app (at the top on the right of Android Studio) to ensure that all dependencies have the necessary versions.

Set up a Firebase Cloud Messaging client app on Android

For frontend developers. Follow the document :

Edit your app manifest. A service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name=""/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>

        <!-- [START fcm_default_icon] -->
        <!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
            android:resource="@drawable/ic_stat_ic_notification" />
        <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. -->
            android:resource="@color/colorAccent" />
        <!-- [END fcm_default_icon] -->
        <!-- [START fcm_disable_auto_init] -->
            android:value="false" />
            android:value="false" />
        <!-- [END fcm_disable_auto_init] -->
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

        <!-- [START firebase_service] -->
                <action android:name="" />
        <!-- [END firebase_service] -->


Access the device registration token

On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

The registration token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

When you need to retrieve the current token, call FirebaseInstanceId.getInstance().getInstanceId(): For example

private void GetRegistrationToken()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "getInstanceId failed", task.getException());

                        // Get new Instance ID token
                        String token = task.getResult().getToken();
                        regToken = token;
                        // Log and toast
                        String msg = getString(R.string.msg_token_fmt, token);
                        Log.d(TAG, msg);
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();

                        new Thread(){
                            public void run(){
                                try {
                                    //SEND REGISTRATION TOKEN TO BACKEND SERVER
                                catch (Exception e) {


As you can see above, after get the registration id you need to pass this to your backend server. In this way you will be able to save this data linking it to your user to permit, in the future, to send message only to selected users.

Android java app with notification from mvc using firebase cloud messaging – part 2

Android java app with notification from mvc using firebase cloud messaging – part 3