Add Push Notifications to Android app with Firebase

Add Push Notifications to Android app with Firebase so that user can get notification whenever you want.

 

Lets Start and make sure you have following::

  • The latest stable Android Studio with your app code
  • A Firebase account, if not then you can create one by signing up with gmail account.

Below are simple steps to do the same –

1> Add a new project or import an existing project to Firebase console -> click on create new project and provide name for the project -> then click on “add firebase to your Android app”

2> Add the package name same as android studio (refer below screenshots) and click on “register App” -> then click on continue which will download json file which we need to copy in adnroid studio project folder.

App name in android studio

App name in firebase

3> Copy the file and paste it in Android project

4> Now go to Android studio and open you app -> go to project level gradle and add classpath as mentioned in below screenshot

classpath ‘com.google.gms:google-services:3.0.0’

5> now go to app level Gradle and add below lines as mentioned in screenshot:

  • Add below line at the end of app level gradle

  apply plugin: ‘com.google.gms.google-services’

  • add below line in dependencies

compile ‘com.google.firebase:firebase-core:9.2.0’ // this line must be included to integrate with Firebase compile ‘com.google.firebase:firebase-messaging:9.2.0’ // this line must be included to use FCM

Last step -> Add services to your app

Two services should be added to use Firebase Cloud Messaging service: a basic code for testing if push notification works, and other codes to handle receiving message or sending message in your app according to your design.

(1) Add a service that extends FirebaseMessagingService

To be able to receive any notification in your app, you should add a service which extends FirebaseMessagingService like this:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
 private static final String TAG = "FCM Service";
 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

Then add it into the AndroidManifest.xml file.

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

(2) Add a service that extends FirebaseInstanceIdService

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

Add it into the AndroidManifest.xml file, this makes sure that the service is loaded

        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

5. Test and send your first push notification!

To see if the setup works, run a test by sending a test message to your own mobile.

enter image description here

(Visited 1,456 times, 2 visits today)

Add a Comment

Facebook