Android sending notification through your app (via Firebase console)

Save

We all love to enable the firebase push notification in our android application.

But while looking at the documents and  prescribed content, one might be confused many a times as in what exactly is the right approach to send a notification.

In this blog, we will learn two ways of sending a push notification to your android application using firbase push notification.

Prerequisite :

  1. Register your application on the firebase console.
  2. Download the google service json file.
  3. Save the server key with you ( this might be required in the second approach).

Now looking at the approaches, the first approach is pretty simple and officially recommended. Lets have a look at this :

Approach 1 :

  1. Put all the content you want to send in a hashmap.
  2. Get an instance of FirebaseMessaging class.
  3. Build an instance of RemoteMessage.Builder class.
  4. Send the notification by using the send method of FirebaseMessaging instance you had.

CODE :

Well, that’s it . You can now send the Push notification.

APPROACH 2 :

  1. Put all your content in a JSON OBJECT .
  2. Create a HTTP request.( you can use any library you wish to i have used VOLLEY).
  3. Change the header of that request.

CODE :

That’s it, the second approach is done.

NOTE: Saving SERVER API KEY in code is not recommended by the FIREBASE.

For getting where exactly thye serverkey is on firebase console ,you can have a look at this screenshot.

 

. . .
Discuss on Helpdesk

Leave a Comment

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


9 comments

  • Muhammad Aashir
    Thank You very much. it is very help full form me….Thank you Thank you….
    • anchit (Moderator)
      You are welcome
      • Muhammad Aashir
        bro lekin … AuthFailureError aa raha hai response se. please help me
        • anchit (Moderator)
          Which Approach are you following?

          Can you share the code you have written?

          • Muhammad Aashir
            2
          • Muhammad Aashir
            second Approach i am use and error appear in volley error response ..
          • anchit (Moderator)
            cross check the headers and are you using the correct API_KEY?
  • Snehil Verma
    how to set notification icon and sound ..please reply !
    • anchit (Moderator)
      for icon and sound you just need to add these fields in your data JSON object.

      data.put(“sound”, YOUR_SOUND_REFRENCE);
      data.put(“icon”, YOUR_ICON_REFRENCE);

      Only these small changes should do.

      If in case these don’t work then you can also try sending these in the notification payload parallel to the data payload only.

      For that, you will have to do something like this :
      JSONObject data = new JSONObject();
      data.put(“title”, YOUR_TITLE);
      data.put(“body”, YOUR_CONTENT);
      data.put(“sound”, YOUR_SOUND_REFRENCE);
      data.put(“icon”, YOUR_ICON_REFRENCE);

      JSONObject notification_data = new JSONObject();
      notification_data.put(“data”, data);
      notification_data.put(“notification”, data);
      notification_data.put(“to”,YOUR_DEVICE_TOKEN_OR_NOTIFICATION_KEY);

      And then use the above-mentioned snippet.

      NOTE: 1) The sound to play when the device receives the notification.

      Supports “default” or the filename of a sound resource bundled in the app. Sound files must reside in /res/raw/.

      2) The notification’s icon.

      Sets the notification icon to myicon for drawable resource myicon. If you don’t send this key in the request, FCM displays the launcher icon specified in your app manifest.

      Thanks,
      Anchit

  • css.php
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home