Firebase Push Notification


Android Working With Firebase Push Notification

Firebase is developed in year 2011 by the company know as Firebase. Than Google acquired this product in 2014. Google added many new features into this product and those new features are introduced in Google I/O 2016. Firebase offers so many useful features. But today we are going to discuss regarding Firebase Push Notification .

For sending Firebase Push Notification we are going to use FCM(Firebase Cloud Messaging). FCM is a newer version of GCM(Google Cloud Messaging). FCM includes all features of GCM and enhanced with some new features. Google is not charging anything for FCM service. We can send push notification to single device or group of devices using FCM. Sending messages 2KB limit or Data Messages 4KB limit.

How Firebase Push Notification works?

Firebase serves as a module between your server and the devices that will be receiving the push notifications that you create. Your server informs Firebase that a notification has to be sent. Then Firebase does the work behind the scenes to get the notification published.

 

Firebase Push Notification Architecture. How firebase pushnotification works?

 

How to integrate FCM into Android Project?

Step for creating  Firebase Project

Step 1: Open Firebase console

How to add new project on firebase console for push notification?

 

Step 2: Click on Add project and give name to your project and select country. If you have already created a project skip this step.

How to create project for sending pushnotification on firebase console?

 

Step 3: Then select “Add Firebase to your Android app”.

How to start android project using firebase console?

 

Step 4: Add project package id or application id and genrate SHA-1 signatature of your machine.

Add SHA key in firebase project

 

How to generate SHA Key using Android Studio:

  1. Open your project into Android studio.
  2. Click on Gradle tab on right side.
  3. Collapse :app module -> Tasks -> Android -> signing report.
  4. SHA-1 will generate. Copy and paste into SHA-1 in firebase console.

How to get SHA key from android studio?

 

Step 5: Download google-service.json and put into app module.

How to get JSON file for firebase push notification?

 

How to integrate Firebase SDK into Android Project?

Step 1:

Add below code into <project>/build.gradle file.

buildscript {
  dependencies {
    // Add this line
    classpath 'com.google.gms:google-services:4.0.0'
  }
}

Step 2:

Add below code into <project> / <app>/build.gradle.

dependencies {
  // Add this line
  implementation 'com.google.firebase:firebase-messaging:17.1.0'
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'

Step 3:

Press on sync now in the Android Studio.

Step 4:

After successfully sync. Edit AndroidManifest.xml or download project files.

A service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, you must extend this service.

 

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

Note: FirebaseInstantIdService” is now deprecated. So no need to include FirebaseInstantIdService in manifeast. Now “FirebaseMessaging” handle all task even for token also.

(Optional)  Within the application component, metadata elements to set a default notification icon and color. Android uses these values whenever incoming messages do not explicitly set icon or color.

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

 

(Optional) From Android 8.0 (API level 26) and higher, notification channels are supported and recommended. FCM provides a default notification channel with basic settings. If you prefer to create and use your own default channel, set default_notification_channel_id to the ID of your notification channel object as shown; FCM will use this value whenever incoming messages do not explicitly set a notification channel.

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>


Now make one class MyFirebaseMessagingService.kt and extend it with FirebaseMessagingService

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.media.RingtoneManager
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat
import android.util.Log
import com.android4dev.pushnotification.R
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import java.util.*


class MyFirebaseMessagingService : FirebaseMessagingService() {

    private val TAG = "MyFirebaseToken"
    private lateinit var notificationManager: NotificationManager
    private val ADMIN_CHANNEL_ID = "Android4Dev"

    override fun onNewToken(token: String?) {
        super.onNewToken(token)
        Log.i(TAG, token)
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        super.onMessageReceived(remoteMessage)
        remoteMessage?.let { message ->
            Log.i(TAG, message.getData().get("message"))

            notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            //Setting up Notification channels for android O and above
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                setupNotificationChannels()
            }
            val notificationId = Random().nextInt(60000)

            val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            val notificationBuilder = NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)  //a resource for your custom small icon
                    .setContentTitle(message.data["title"]) //the "title" value you sent in your notification
                    .setContentText(message.data["message"]) //ditto
                    .setAutoCancel(true)  //dismisses the notification on click
                    .setSound(defaultSoundUri)

            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build())

        }

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private fun setupNotificationChannels() {
        val adminChannelName = getString(R.string.notifications_admin_channel_name)
        val adminChannelDescription = getString(R.string.notifications_admin_channel_description)

        val adminChannel: NotificationChannel
        adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW)
        adminChannel.description = adminChannelDescription
        adminChannel.enableLights(true)
        adminChannel.lightColor = Color.RED
        adminChannel.enableVibration(true)
        notificationManager.createNotificationChannel(adminChannel)
    }
}


onNewToken(token: String?)
this method called when the system determines that the tokens need to be refreshed. In old FirebaseMessaging Version we are using onTokenRefresh () for this purpose and we are using another service called FirebaseInstanceIdService for generating token now no need to do that. Now FirebaseMessagingService will handle everything.

fun onMessageReceived(remoteMessage: RemoteMessage?) this method will called when notification will received from FirebaseServer.

Now make one Activity FirebasePushNotificationActivity.kt

import android.os.Bundle
import android.util.Log
import com.android4dev.pushnotification.base.BaseActivity
import com.google.firebase.iid.FirebaseInstanceId
import java.io.IOException

class FirebasePushNotificationActivity : BaseActivity() {
    private val TAG = "MyFirebaseToken"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_firebase_push_notification)
        initView()
    }

    private fun initView() {
        //This method will use for fetching Token
        Thread(Runnable {
            try {
                Log.i(TAG, FirebaseInstanceId.getInstance().getToken(getString(R.string.SENDER_ID), "FCM"))
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }).start()
    }
}

 

FirebaseInstanceId.getInstance().getToken(getString(R.string.SENDER_ID),”FCM”) this method is used for fetching token. Always run this method in new Thread don’t use on main thread.

 

How to Send Firebase Push Nofitication Using Postman?

Step 1:

Using https://fcm.googleapis.com/fcm/send  url we can send push notification.Url for sending firebase pushnotification?

 

In, Authorizationyou need to pass “key= Server key”. You will get Server Key from Firebase Console . Check below image if you don’t know where you can find Server Key.

How to get server key from firebase console?

Step 2:

In the body of your request you need to specify the “to” field to send a notification to the targeted device. It’s value is the aforementioned Firebase token that your device obtains in MyFirebaseMessagingService. or you can obtain token using FirebaseInstanceId.getInstance().getToken(getString(R.string.SENDER_ID),”FCM”) method.

How to send firebase pushnotification using postman?

 

{
  "to":
    "Add your device token",
  "data": {
    "title": "Android4Dev",
    "message": "Learn Firebase PushNotification From Android4Dev Blog"
  }
}

 

Finally you will get notification on your device.

 

Pushnotification in android device


Tips for Handling Push while app in background or killed state.

Use data payload instead of notification while sending push. Firebase handle data message even if the app is in background/killed state and get the data into MyFirebaseMessagingService/onMessageReceived() method. This will get called every time when push has only data payload.Data can be access directly by calling RemoteMessage.data() and it returns Map<String,String>.

 

Steps for sending Firebase Push Notification from php:

  1. Click on the newly added project, in the upper left menu is the “Overview” and Gear Settings.
  2. Click on the GEAR settings icon, and then on “Project Settings”
  3. In the main screen, click on “Cloud Messaging”
  4. Here you will find both your “Server Key” and “SenderID”
  5. The “Server Key” is what is used below as the API_ACCESS_KEY
  6. The “SenderID” is what your app (on the client phone) will need to generate a ‘registration_ID’
  7. You now need to register the “senderID” in your developer play console of your app
  8. Go to: https://play.google.com, login to manage your app on the playstore
  9. Click on the specific app and in the left menu under “Development Tools” click on “Serivces & APIs”
  10. Under the Firebase Cloud Messaging (FCM) section, click “Link Sender ID”, add your “server key” and click “link”. After it links, your “senderID” will appear in the linked section.
  11. In your App, (I use Cordova => phonegap-plugin-push plugin), make a call the FCM server, passing the senderID
  12. The client App will receive the “registration_ID”. – this is a unique ID that specifically registers the users phone to receive messages on behalf of your app.
  13. Your app will need to pass the ‘registration_ID’ to your server. In my case, my app passes it via an api call. But your app could simply pass a silent url call to your server to capture the users ‘registration_ID’ (iehttp://yourserver.com/passID.php?userID=jdoe&regID=$registration_ID).
  14. Ideally, you will want to store that registration_ID locally on your server (mysql).
  15. In the future, when you need to push a notification to that user, call up the stored registration_ID and pass it to the script below:

NOTE: Once you register your app in the FCM console and retrieve your Server Key and SenderID,  it might take 12 to 24 hours for the IDs to propagate to the FCM messaging servers. I experienced similar case in my application after registering my code worked but phone never received the messages after 10 hours later.I have not made any changes to my code and phone suddenly started getting the messages. I assume it was a propagation issue.

Follow me on twitter for latest android blogs and news. If you you need any help please ask me in comments and feel free to message me on facebook or twitter.

Summary

Hope you will now understand how Firebase Push Notification works in android. Download source code for detail code implementation. Follow me on twitter or Instagram for new blogs related information.

Source Code

Demo Project For Android Firebase PushNotification

 

Hi, I am Android Developer and Founder of Android4Dev. Right now my interest is in RxKotlin, MVVM and Dagger2. I am Kotlin Lover. Kotlin made me more productive in terms of coding. Do you want me to code for you ? Please don’t hesitate to contact me.