Push Notification Using Firebase Cloud Messages

A push notification is a message which is triggered on mobile devices. Push notification is a message you can display to the user outside of your application’s normal UI. Push notifications look like SMS text messages and mobile alert notifications. Push notifications reach to users only when your app is installed in the device.

How to integrate FCM in an Android app?

1. Create a Firebase at Firebase Console.

  • Explore a console.firebase.google.com at your browser.

  • Click Add project, enter Project name If you have an existing Google Cloud Platform (GCP) project, you can select the project from the dropdown menu to add Firebase resources to that project.
  • Enter projected this field is optional because Firebase automatically assigns new unique id for the project.

  • Click Continue

  • Click Continue.
  • After that set the google analytics for your project this field is optional
  • Click Create Project

2. Register your app with Firebase

  • At Firebase Console Click the android icon at the project overview page.

  • Enter your app package from the manifest file.
  • (Optional step) Enter other information like App nickname and Debug signing certificate SHA-1.

  • Click Register App

3. Download the google-services.json and add this file into your app module.

4. Add the Firebase SDK

  • Project-level build.gradle
buildscript {

  repositories {

// Check that you have the following line (if not, add it):

google()  // Google's Maven repository

  }

  dependencies {

...

// Add this line

classpath 'com.google.gms:google-services:4.3.4'

  }

}

allprojects {

  ...

  repositories {

// Check that you have the following line (if not, add it):

google()  // Google's Maven repository

...

  }

}
  • App-level build.gradle
apply plugin: 'com.android.application'

// Add this line

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

dependencies {

  // Import the Firebase BoM

  implementation platform('com.google.firebase:firebase-bom:26.1.0')

  // Add the dependency for the Firebase SDK for the Firebase Cloud Messaging and Google Analytics

  // When using the BoM, don't specify versions in Firebase dependencies  

  implementation 'com.google.firebase:firebase-messaging'

  implementation 'com.google.firebase:firebase-analytics'

}
  • Click Sync Now appears at the right side in your android studio IDE.

5. Edit your App Menifest

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

package="com.example.myproject">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application

android:name=".MyApplication"

android:allowBackup="false"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:networkSecurityConfig="@xml/network_security_config"

android:roundIcon="@mipmap/ic_launcher"

android:screenOrientation="portrait"

android:supportsRtl="true"

android:theme="@style/AppTheme"

tools:ignore="HardcodedDebugMode">

<activity

android:name=".MainActivity"

android:theme="@style/AppThemeNoActionBar">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service

android:name=".MyFirebaseServices"

android:exported="false">

<intent-filter>

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

</intent-filter>

</service>

<meta-data

android:name="com.google.firebase.messaging.default_notification_channel_id"

android:value="@string/notification_channel_id" />

<meta-data

android:name="com.google.firebase.messaging.default_notification_icon"

android:resource="@drawable/ic_app_logo" />

<meta-data

android:name="com.google.firebase.messaging.default_notification_color"

android:resource="@color/colorAccent" />

</application>

</manifest>

6. Create A service class which extends FirebaseMessagingService

public class MyFirebaseServices extends FirebaseMessagingService {

@Override

public void onNewToken(@NonNull String refreshToken) {

Log.d(TAG, "Refreshed token: " + refreshToken);

// you need to send this token to server for device identification which will helpful to send the notification from server to devices...

sendThisTokenToServer(refreshToken);

}

@Override

public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {

Log.d(TAG, "Remote Message received");

super.onMessageReceived(remoteMessage);

}

}

7. Retrieve Current registration token

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getName();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

getTheCurrentToken();

}

private void getTheCurrentToken() {

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {

@Override

public void onComplete(@NonNull Task<String> task) {

if(!task.isSuccessful()) {

Log.w(TAG, "Fetching FCM registration token failed", task.getException());

return;

}

// send this token to your server with the help of web services apis...

String token = task.getResult();

Log.d(TAG, token);

Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();

}

});

}

}

 

Leave a Reply