Skip to main content

Working with Google Firebase Part-3 Push Notification

Hello friends,

In the first part of this series we learn how to register our application on Firebase Console. In second part we learn about firebase authentication. In this part we will learn how to send push notification to our android application using Firebase.

As we know we can send push notifications using Google Cloud Messing(GCM) too. But configuring our application for GCM to receive push notifications is complex. 
In compare to GCM configuring push notification in our application is very easy. You have to just register you application on firebase console and create you application structure successfully. If you don't go through that how to register our application to Firebase Consoleplease go through this linkwhich is the first part of this series describe how to register our application to Firebase Console.

If you have done with application configuration, then you 80% done. Now you have to just create two service classes, one extending FirebaseMessagingService and second extending FirebaseInstanceIdService. Now register your created service classes to you manifest.xml. You are done all with configuration for firebase push notification.

NOTE: I will not explain how to implement it on server side. I will so example to sending notification using Firebase Console.


Ok, Now add this gradle line to your build.gradle file:
              compile 'com.google.firebase:firebase-messaging:9.2.0'

Create two service classes One for getting notification token id, which is used for server side notification implementation and Second for receive notifications.

MyFirebaseInstanceIDService.java

import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    public MyFirebaseInstanceIDService() {
    }
    @Override
   
public void onTokenRefresh() {
     // Get updated InstanceID token.
       
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d("firebase", "Refreshed token: " + refreshedToken);
       //This token is used by server side so use below method to send token to server
     // sendRegistrationToServer(refreshedToken);
   
}
}

MyFirebaseMessagingService.java

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import com.doird.amar.myfirebaseimpl.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
   
public MyFirebaseMessagingService() {
    }

   
@Override
   
public void onMessageReceived(RemoteMessage remoteMessage) {
       
super.onMessageReceived(remoteMessage);
        Log.d(
"firebase", "Notification Message: " +
                remoteMessage.getNotification().getBody());
        showNotification(remoteMessage.getNotification().getBody());
    }

   
private void showNotification(String msg) {
        NotificationCompat.Builder builder =
new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.
ic_launcher);
        Intent intent =
new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.
ic_launcher));
        builder.setContentTitle(
"Firebase Push Notification");
        builder.setContentText(msg);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
        notificationManager.notify(
1, builder.build());
    }
}

And now add these two classes to your manifest.xml file like this:-

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


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

You are done with firebase notification work. Now go to  Firebase Console and select your registered application. Look for a Notification option in left panel and click.
Compose your notification message and send. Look this link for more clearance. Best of Luck :)




Comments

  1. Thanks , article is helpful for implementing FCM :)

    ReplyDelete
  2. That's amazing informative post, I want to add one more thing, If you want to make your web visitor to your subscriber then you should definitely check gravitec lifetime deal Best push notification for website ever.

    ReplyDelete

Post a Comment

You are responsible person and please write responsibly

Popular posts from this blog

JavaFX WebView- Creating Browser Sample

Hi Friends, In this post, i am going to give an overview about JavaFX WebView . This is an embedded browser component which is based on WebKit . If allow you to use Css, JavaScript, HTML5 and more to customise your embedded browser. The embedded browser enables you to perform the following tasks in your JavaFX applications: Render HTML content from local and remote URLs Obtain Web history Execute JavaScript commands Perform upcalls from JavaScript to JavaFX Manage web pop-up windows Apply effects to the embedded browser  I am going to provide and explain you a sample example to create your embedded browser. This is a JavaFX sample example. if you want to take an introduction about JavaFX please visit my previous blog . I have use IntelliJ Idea IDE for this example. You can visit this link  to understand how to create JavaFX application. I am attaching the project structure image below- In this sample- we have two java class. First

Working with MPAndroidChart (how to create Bar Chart using MPAndroidChart)

Hi Friends, In this tutorial i am going to show, "How to create Bar Chart using MPAndroidChart". There is a lot of libraries for creating charts in android like AChartEngine, MpAndroidChart, AndroidPlot etc. Your first question may be, Why MPAndroidChart. So MpAndroidChart provides better animation functionality and easy to use in comparision. Using  MPAndroidChart library  we can draw a: ·          Simple Bar Chart ·          Grouped Bar Chart ·          Horizontal Bar Chart ·          Simple Line Chart ·          Line Chart with Cubic Lines ·          Grouped Line Chart ·          Combined Line and Bar Chart ·          Pie Chart ·          Scatter Chart ·          Candlestick Chart ·          Radar Chart Here we will create Simple Bar Chart with multiple color based on ranging. Ok We start with coding part now. First of all create your project in Android Studio and add gradle dependency for  MPAndroidChart library  into y

app-policy

PRIVACY POLICY Last updated April 19, 2023 This privacy notice for Team CoderzDuniya ( " Company ," " we ," " us ," or " our " ), describes how and why we might collect, store, use, and/or share ( " process " ) your information when you use our services ( " Services " ), such as when you: Download and use our mobile application ( Revenue Calculator) , or any other application of ours that links to this privacy notice Engage with us in other related ways, including any sales, marketing, or events Questions or concerns?  Reading this privacy notice will help you understand your privacy rights and choices. If you do not agree with our policies and practices, please do not use our Services. If you still have any questions or concerns, please contact us at droidamar007@gmail.com . SUMMARY OF KEY POINTS This summary provides key points from our privacy notice, but you can find out more details about any of these t