Menu Close

5 steps to easily integrate FCM in your android app

Firebase Cloud Messaging (FCM) is a cross platform messaging solution by Google. Since Google Cloud Messaging (GCM) was deprecated on April 10, 2018 (GCM servers will be removed by May 29, 2019), a lot of android developers are trying to switch over to FCM. Today we look at the steps involved in integrating FCM in your android app:

1) Create a sample android app

Open your Android Studio and create a new project. Name it SampleFCM. Change the company domain to apnstester.com.

2) Register your app with firebase

  • Login to Firebase.
  • Click on Add Project, give it a name and click Create Project.
  • In the getting started window, click on the android icon.
  • Enter your app’s package name (from build.gradle). For our sample app the package name is com.apnstester.samplefcm.
  • Click Register App.
  • Download the google-services.json file. Put that file in your project’s app folder (in android studio).

3) Add dependencies to your build.gradle

  • Open your app level build.gradle file (SampleFCM/app/build.gradle).
  • Add the firebase dependency and google services plugin.
dependencies{

    // other dependencies

    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-messaging:17.6.0'


}
apply plugin: 'com.google.gms.google-services'
  • Open the root build.gradle ( SampleFCM/build.gradle ) and add the below line under dependencies.
dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'

        classpath 'com.google.gms:google-services:4.0.1' // ADD THIS LINE
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
fcm google-services classpath

3) Creating the Firebase Service

  • Create a new java class and name it MyFirebaseService.
  • Make the class extend FirebaseMessagingService.
  • Override the onMessageReceived method and fill in the code like below:
public class MyFirebaseService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);


Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());
}
}

4) Add the service to your AndroidManifest.xml file

All services that your use in your android code need to be explicitly mentioned in the manifest file. Add the below code to your AndroidManifest.xml file under the <application> tag.

<service
android:name=".MyFirebaseService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

5) Testing your code

  • Add the below code to the onCreate method of your MainActivity to get the device token:
Log.d("fcmtoken",FirebaseInstanceId.getInstance().getToken());
  • Connect your android device and run the application on it (you cannot test FCM on a simulator).
  • In logcat window, search for fcmtoken. Copy and save the token. We will need it later.
  • In your firebase console, go to Project Settings -> Cloud Messaging and copy the server key.
  • Go to https://apnstester.com/fcm and paste your server key and fcm token there.
  • In message, enter “Testing FCM with apnstester.com” .
  • Make sure your app is in background so that your can see the notification show up on your phone. ( app in background means close the application or press the home button on your android device).
  • Click Test.
  • Check your android device and verify that you have received a notification.

Please follow and like us:
error

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *