{"id":39183,"date":"2016-08-18T13:34:04","date_gmt":"2016-08-18T08:04:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=39183"},"modified":"2017-09-08T12:54:53","modified_gmt":"2017-09-08T07:24:53","slug":"push-notifications-using-firebase-cloud-messaging","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/push-notifications-using-firebase-cloud-messaging\/","title":{"rendered":"Push notifications using Firebase Cloud Messaging"},"content":{"rendered":"<p>Assuming that you are familiar with <strong>Google Cloud Messaging<\/strong>, the\u00a0new word for you in the title <strong>\u201cFirebaseCloudMessaging\u201d<\/strong>\u00a0or &#8220;<strong>FCM&#8221;<\/strong> is\u00a0<strong><em>Firebase<\/em><\/strong>.<\/p>\n<p>In case you don&#8217;t know about Firebase, let me give you a brief idea\u00a0about it.\u00a0<strong>Firebase<\/strong> is a cloud service provider, provided by Google, which\u00a0provides <a href=\"http:\/\/www.tothenew.com\/blog\/infographics-building-mobile-apps-with-mbaas\/\" target=\"_blank\">Mobile backend as a service (MBaaS)<\/a>, also known as\u00a0<em>&#8220;backend as a service&#8221; (BaaS)<\/em>. Using Firebase, you can quickly sync\u00a0up your data and make it available it on your app, which is being\u00a0used by multiple users.<\/p>\n<p>Now, since it is being used by multiple\u00a0users, how does Firebase individualizes multiple users? Here comes\u00a0the role of <strong>\u201cauthentication\u201d<\/strong><em> (mechanism of providing\u00a0identification to\u00a0each user)<\/em>\u00a0by Fireabase. Since authentication plays a vital role\u00a0while sending <a href=\"http:\/\/www.tothenew.com\/blog\/push-notification-in-android-using-grails\/\" target=\"_blank\">push notifications<\/a>, let me brief you about it as well.<\/p>\n<p>What is <strong>authentication<\/strong> and role of <strong>FireBase<\/strong> in it?<\/p>\n<p>Generally applications need to know the identity of their user. Being aware of\u00a0user&#8217;s identity allows <a title=\"android application development services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\" target=\"_blank\">android apps<\/a> to provide them a customized experience and grant\u00a0them permissions for accessing their data. This process of proving a user&#8217;s\u00a0identity is called <strong>authentication.<\/strong> Firebase provides a full set of\u00a0authentication options.<\/p>\n<p>When a user authenticates to a Firebase app :<\/p>\n<ul>\n<li>Information about the<em> user<\/em> is returned in callbacks on the client device.\u00a0Using this we can customize our app&#8217;s user experience, according to a specific user.<\/li>\n<li>This user information returned contains a <em>UID<\/em> (a unique ID), which is\u00a0distinct across all providers, and it never changes for a specific\u00a0authenticated user.<\/li>\n<li>Now, in our app&#8217;s security &amp; Firebase rules, value for the <strong><em>authToken<\/em>\u00a0<\/strong>becomes defined. Its value will be null for unauthenticated users.\u00a0However, for authenticated users, it will an object containing <em>auth.uid\u00a0(unique for every user)<\/em> and some other informational data\u00a0about the user. Using this, we can securely control data access on a per-user basis.<\/li>\n<\/ul>\n<p>What is <strong>FCM<\/strong>?<\/p>\n<p>Firebase Cloud Messaging (FCM) provides a cross-platform messaging solution\u00a0that allows reliable delivery of messages at no cost.\u00a0Using this we can :<\/p>\n<ul>\n<li>Send notification messages (2KB limit) or data messages (4KB limit).<\/li>\n<li>Distribute messages to a single device, groups of devices, or to devices\u00a0subscribed to some topics.<\/li>\n<li>Send acknowledgments, chats, and other messages from devices back to\u00a0the server over FCM\u2019s reliable and battery-efficient connection channel.<\/li>\n<\/ul>\n<p>Using Notification messages, <a title=\"remote android developer\" href=\"http:\/\/www.tothenew.com\/mobile-ios-application-development-services\">developers<\/a> can send users a visible display\u00a0message with some predefined keys and optional custom key-value pairs\u00a0(data payloads).<\/p>\n<p>Apart from this, <strong>what more Google has to say about FCM?<\/strong><\/p>\n<p>If you go to URL &#8211; https:\/\/developers.google.com\/cloud-messaging\/\u00a0 to learn about GCM,<br \/>\nthe very first lines that will strike you are :<\/p>\n<p><span style=\"color: #000000;\"><strong>Firebase Cloud Messaging(FCM) is the new version of GCM. It\u00a0inherits the reliable and scalable GCM infrastructure, plus new\u00a0features! If you are integrating messaging in a new app, start with\u00a0<\/strong><strong>FCM. GCM users are strongly recommended to upgrade to FCM, in\u00a0order to benefit from new FCM features today and in the future.<\/strong><\/span><\/p>\n<p>As you can deduce from this statement, Google is investing strongly in\u00a0Firebase, to make it one unified mobile platform.<\/p>\n<p>Lets clear few points before proceeding further :<\/p>\n<p>How is FCM <em><strong>similar<\/strong><\/em> to GCM?<\/p>\n<p>Both can be used to send messages to multiple platforms \u2013 Android, iOS, and\u00a0Chrome, as their core structure is same.<\/p>\n<p>How is FCM <em><strong>different<\/strong> <\/em>from GCM?<\/p>\n<ul>\n<li>Unlike GCM integration, we <em>don&#8217;t<\/em> have to write our own registration or\u00a0subscription retry logic.<\/li>\n<li>We <em>no longer<\/em> have to explicitly declare \u201cReceiver\u201d.<\/li>\n<li>Generation of registration token is handled by the library itself. We <em>don&#8217;t\u00a0<\/em>have to initiate it <em>explicitly.<\/em><\/li>\n<li>It provides <em>server-less<\/em> notifications solution with a web console &#8211;\u00a0Firebase Notifications. This web console will remind you of Parse\u00a0Push Console (if you have used it).<\/li>\n<\/ul>\n<p>So is GCM going to be deprecated?<\/p>\n<p><strong>No.<\/strong> However, some features of GCM are already depreciated. Like &#8211;\u00a0Obtaining Registration Tokens with <em>gcm.register()<\/em> is deprecated now. So, in\u00a0coming days some other functions may also get depreciated. However, they\u00a0will support the current version of GCM Android and iOS SDKs.\u00a0All new client-side features will be added to FCM SDKs only.<\/p>\n<p><strong>Prerequisites\u00a0<\/strong>for using FCM<\/p>\n<ul>\n<li>A device running on Android 2.3 (Gingerbread) or newer version, along with\u00a0Google Play services 9.2.1 or newer.<\/li>\n<li>Android Studio 1.5 or higher version.<\/li>\n<li>An Android Studio project and its package name.<\/li>\n<\/ul>\n<p><strong>Steps<\/strong> for Integrating Firebase and FCM SDK<\/p>\n<ol>\n<li>Add Firebase to your app<\/li>\n<\/ol>\n<p>Go to Firebase console and either create a new project or import\u00a0an existing one.\u00a0After creating or importing your project, a welcome screen will open;\u00a0like below.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-39215 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase-10.png\" alt=\"firebase 10 push notification console\" width=\"1366\" height=\"768\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase-10.png 1366w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase-10-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase-10-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase-10-624x350.png 624w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/p>\n<p>As I&#8217;m into Android, I&#8217;ll be adding Firebase to my Android app.\u00a0However, steps for Android, iOS and Web app are almost same.<\/p>\n<p>Click on Add Firebase to your app.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-39216 size-full aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase12.png\" alt=\"firebase12\" width=\"1366\" height=\"768\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase12.png 1366w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase12-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase12-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase12-624x350.png 624w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/p>\n<p>Add package name by copying it from <em>AndroidManifest.xml<\/em> file and get\u00a0<em>SHA-1<\/em> key by using below command :<\/p>\n<p><strong><em>$ keytool -exportcert -alias androiddebugkey -keystore\u00a0~\/.android\/debug.keystore -list -v<\/em><\/strong><\/p>\n<p>It may ask for keystore password: Type &#8220;android&#8221; if using <em>debug.keystore.<\/em>\u00a0After entering required information click on\u00a0<strong>Add App.<\/strong>\u00a0Now it&#8217;ll ask you to save downloaded <em>google-services.json<\/em> file. Save it in\u00a0the <strong>root of app folder<\/strong> of your app. Next, you&#8217;ll be asked to add the following dependencies\u00a0to your <em>gradle<\/em> file for accessing Firebase SDKs.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-39218 size-full aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase14.png\" alt=\"firebase14\" width=\"1366\" height=\"768\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase14.png 1366w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase14-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase14-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase14-624x350.png 624w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/p>\n<p>Instead of\u00a0adding complete client SDK, you should add dependencies for the Firebase\u00a0SDKs you wish to use. Like in our case, its<\/p>\n<p><em><span style=\"color: #000000;\">compile<\/span><span style=\"color: #008000;\"> &#8216;com.google.firebase:firebase-messaging:9.2.1&#8217;<\/span><\/em><\/p>\n<p>Sync your project.<br \/>\nIf you are getting a build error complaining about duplicate files you can\u00a0choose to exclude those files by adding the<em> packagingOptions<\/em> directive to\u00a0your<em> build.gradle<\/em> file, inside the android domain:<\/p>\n<p><span style=\"color: #000000;\">packagingOptions{<\/span><br \/>\n<span style=\"color: #000000;\">exclude<\/span><span style=\"color: #008000;\"> &#8216;META-INF\/LICENSE&#8217;<\/span><br \/>\n<span style=\"color: #000000;\">exclude<\/span><span style=\"color: #008000;\"> &#8216;META-INF\/LICENSE-FIREBASE.txt&#8217;<\/span><br \/>\n<span style=\"color: #000000;\">exclude<\/span><span style=\"color: #008000;\"> &#8216;META-INF\/NOTICE&#8217;<\/span><br \/>\n<span style=\"color: #000000;\">exclude<\/span><span style=\"color: #008000;\"> &#8216;LICENSE.txt&#8217;<\/span><br \/>\n<span style=\"color: #000000;\">}<\/span><\/p>\n<p>Click on finish and you are done. <strong>Firebase is added to your app now.<\/strong><\/p>\n<p>Now let us first understand <strong>how Firebase works ?<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Client app registration process &#8211;\u00a0<\/strong><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-39189 size-full aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firrebase1.png\" alt=\"Client registration process\" width=\"660\" height=\"685\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firrebase1.png 660w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firrebase1-289x300.png 289w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firrebase1-624x647.png 624w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Send notifications using FCM<\/strong><\/span><\/p>\n<div id=\"attachment_39190\" style=\"width: 684px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-39190\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-39190 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase2.png\" alt=\"Sending notification\" width=\"674\" height=\"742\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase2.png 674w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase2-272x300.png 272w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase2-624x686.png 624w\" sizes=\"(max-width: 674px) 100vw, 674px\" \/><p id=\"caption-attachment-39190\" class=\"wp-caption-text\">Message received in the form of notification<\/p><\/div>\n<p>Since, you have understood by now how FCM works, lets begin some coding.<\/p>\n<ol>\n<li>Add internet permission to your manifest file. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;<\/span><span style=\"color: #000080;\">uses-permission<\/span>\u00a0<span style=\"color: #800080;\">android<\/span><span style=\"color: #0000ff;\">:name=<\/span><span style=\"color: #008000;\">&#8220;android.permission.INTERNET&#8221;<\/span> <span style=\"color: #000000;\">\/&gt;<\/span><\/li>\n<li>Now we need to add a service that extends <strong>FirebaseMessagingService.\u00a0<\/strong>This is the Base class for communicating with Firebase Messaging.\u00a0This FirebaseMessagingService<strong> extends Service class. <\/strong><span style=\"color: #000000;\">Class hierarchy followed is : \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/span><\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-39191 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase4.png\" alt=\"firebase4\" width=\"551\" height=\"147\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase4.png 551w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/firebase4-300x80.png 300w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/p>\n<p>This class\u00a0provides various functionalities :<\/p>\n<ul>\n<li>automatically displaying notifications.<\/li>\n<li>message handling beyond receiving notifications on apps in the\u00a0background.<\/li>\n<li>receive notifications in foregrounded apps<\/li>\n<li>receiving data payload<\/li>\n<li>sending upstream messages<\/li>\n<li>receiving errors in case of upstream message failures.<\/li>\n<\/ul>\n<p>To handle any type of events required by the application, we need to override\u00a0this base class methods. These methods are invoked on a background thread.\u00a0To add this service include following to your manifest file :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-39193 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-51.png\" alt=\"Firebase 5\" width=\"959\" height=\"234\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-51.png 959w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-51-300x73.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-51-624x152.png 624w\" sizes=\"(max-width: 959px) 100vw, 959px\" \/><\/p>\n<p>3. Now we need to add a service that extends <strong>FirebaseInstanceIdService<\/strong>.\u00a0This is the Base class to handle Firebase Instance ID token refresh events.\u00a0This FirebaseInstanceIdService <strong>extends Service class.<\/strong><\/p>\n<p>Class hierarchy followed is :<\/p>\n<h3><span style=\"color: #000000;\">1<\/span><span style=\"text-decoration: underline;\"><span style=\"color: #000080; text-decoration: underline;\"> java.lang.Object<\/span><\/span><br \/>\n<span style=\"color: #000000;\">2 \u21b3<\/span><span style=\"text-decoration: underline;\"><span style=\"color: #000080; text-decoration: underline;\">android.content.Context<\/span><\/span><br \/>\n<span style=\"color: #000000;\">3 \u21b3<\/span><span style=\"text-decoration: underline;\"><span style=\"color: #000080; text-decoration: underline;\">android.content.ContextWrapper<\/span><\/span><br \/>\n<span style=\"color: #000000;\">4 \u21b3android.app.Service<\/span><\/h3>\n<p><strong>It provides various functionalities :<\/strong><\/p>\n<ul>\n<li>Creation of registration tokens<\/li>\n<li>Rotation of registration tokens<\/li>\n<li>Update process of registration tokens<\/li>\n<li>Sending notification to a single device or group of devices<\/li>\n<li>Allows devices to subscribe to various topics with the server subscription\u00a0management API.<\/li>\n<\/ul>\n<p>If your app requires Instance ID or uses Instance ID tokens, then we can\u00a0extend this class and implement<strong> onTokenRefresh()<\/strong> to receive token\u00a0refresh events.\u00a0To add this service include following to your manifest file :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-39194 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-6.png\" alt=\"firebase cloud messaging server java\" width=\"1031\" height=\"469\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-6.png 1031w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-6-300x136.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-6-1024x465.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-6-624x283.png 624w\" sizes=\"(max-width: 1031px) 100vw, 1031px\" \/><\/p>\n<p>Now create the respective service classes in your Android code.<\/p>\n<p><strong>Retrieving &amp; monitoring of device registration token<\/strong><\/p>\n<p>On initial\u00a0startup of our app, the FCM SDK will generate a registration token\u00a0for the client app instance. If we want to target a single device or create device\u00a0groups, we&#8217;ll need this token which can be retrieved from our\u00a0MyFirebaseInstanceIDService class. This token could be rotated or changed\u00a0after initial startup. So, I would strongly recommended you to fetch the latest\u00a0updated registration token using<em>\u00a0 \u00a0<span style=\"text-decoration: underline;\">FirebaseInstanceId.getInstance().getToken()<\/span> ;\u00a0\u00a0<\/em>\u00a0unless you specifically need to retrieve the current token.\u00a0So to retrieve the token, simply override <em>onTokenRefresh()<\/em> as \u00a0below:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-39195 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-7.png\" alt=\"push notification in android using firebase\" width=\"1035\" height=\"469\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-7.png 1035w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-7-300x135.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-7-1024x464.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-7-624x282.png 624w\" sizes=\"(max-width: 1035px) 100vw, 1035px\" \/><\/p>\n<p>If the system determines that the token needs to be refreshed, then this\u00a0callback is called. Once the token is retrieved, you can save it to your server\u00a0and store it using your required method.\u00a0This will not be called very frequently. It is needed for key rotation and to\u00a0handle Instance ID changes; which happens in following cases:<\/p>\n<ul>\n<li>When app deletes Instance ID<\/li>\n<li>When app is restored on a new device<\/li>\n<li>When user re-installs or uninstalls the app<\/li>\n<li>When user deletes the app data<\/li>\n<\/ul>\n<p>If you have specific need to retrieve current token call\u00a0call <strong>FirebaseInstanceID.getToken()<\/strong>. It returns <strong>null<\/strong> if the token has not yet been generated.<\/p>\n<p>Once this is set up we are ready to start <a title=\"sending downstream messages with the Firebase console\" href=\"https:\/\/firebase.google.com\/docs\/notifications\/android\/console-audience\" target=\"_blank\">sending downstream messages with the Firebase<\/a><br \/>\n<a title=\"sending downstream messages with the Firebase console\" href=\"https:\/\/firebase.google.com\/docs\/notifications\/android\/console-audience\" target=\"_blank\">console<\/a> .<em> (Downstream messages means sending messages from your app\u00a0<\/em><em>server to Firebase Cloud Messaging client apps).\u00a0<\/em>However, lets skip that part for now. Let us first understand how to handle\u00a0received messages on client side, otherwise some users may not receive the\u00a0notifications we send. This includes any users who have your app in the\u00a0foreground on the device.<\/p>\n<p><strong>Receiving and handling messages<\/strong><\/p>\n<p>In order to receive messages, we have to override\u00a0the <strong>onMessageReceived()<\/strong> callback from our FireBaseMessagingService\u00a0class; keeping in mind the exceptions mentioned below :<\/p>\n<ul>\n<li><strong>Notifications delivered when your app is in the background<\/strong> &#8211; In\u00a0this case, notification is delivered to the system tray of the device. A user&#8217;s tap\u00a0on a notification opens the app launcher by default.<\/li>\n<li><strong>Messages with notification + data payload &#8211;<\/strong>\u00a0In this case, the\u00a0notification is delivered to the device\u2019s system tray, and data payload is\u00a0delivered in the extras of the intent of your launcher Activity.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-39196 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-8.png\" alt=\"firebase send notification from device\" width=\"959\" height=\"681\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-8.png 959w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-8-300x213.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase-8-624x443.png 624w\" sizes=\"(max-width: 959px) 100vw, 959px\" \/><\/p>\n<p>This is also called if any notification message is received while user is interacting with the app i.e. app is in the foreground. To fetch notification parameters use <a title=\"getNotification() \" href=\"https:\/\/developers.google.com\/android\/reference\/com\/google\/firebase\/messaging\/RemoteMessage#getNotification()\" target=\"_blank\">getNotification()<\/a> method.If we need to perform some action based on the message received and access message data, we will have to override this method.<\/p>\n<p><strong>How to send a message from the FireBase console?<\/strong><\/p>\n<ol>\n<li>Install and run the app on the target devices.<\/li>\n<li>Open the <strong>Notifications<\/strong> tab of the Firebase console and select<strong> New\u00a0Message<\/strong>.<\/li>\n<li>Enter message text.<\/li>\n<li>Select the message target (devices). The dialog displays further advanced options\u00a0to filter the target devices by allowing you to choose from <strong>App\u00a0Version, Users in Audience or Device Language.<\/strong><\/li>\n<\/ol>\n<div id=\"attachment_39198\" style=\"width: 635px\" class=\"wp-caption alignleft\"><img aria-describedby=\"caption-attachment-39198\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-39198 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase91-1024x568.png\" alt=\"FireBase console to send messages web push notification example\" width=\"625\" height=\"346\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase91-1024x568.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase91-300x166.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase91-624x346.png 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Firebase91.png 1182w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-39198\" class=\"wp-caption-text\">FireBase console to send messages<\/p><\/div>\n<p>Once you click <strong>Send Message<\/strong> button, client devices you targeted, that have\u00a0the app in the background, will receive notification in the system notifications\u00a0tray. Whenever the user will tap on the notification, the app launcher will\u00a0open up your app.\u00a0If you want to send a message to a particular device: select \u201cSingle Device\u201d\u00a0from options available and insert the token you received.<\/p>\n<p>Various other advanced options are available which you can explore yourself\u00a0by playing around with them like :<\/p>\n<ul>\n<li>the<strong> expiration period<\/strong><\/li>\n<li><strong>play a sound<\/strong> when the notification is received<\/li>\n<li>Setting custom data with some message<\/li>\n<li>setting the notification priority \u2013 normal or high<\/li>\n<\/ul>\n<p><strong>Normal priority \u2013<\/strong> This is the default priority for message delivery. These\u00a0messages do not open up network connections if your device in is \u201cDoze\u00a0mode\u201d, and their delivery is delayed to preserve the battery.<\/p>\n<p><strong>High priority<\/strong> &#8211; FCM delivers high priority messages immediately,\u00a0permitting the FCM service to wake a device which is in \u201cdoze mode\u201d when\u00a0possible and open a network connection to your app server. Apps which have\u00a0features like instant messaging, chat, or voice call alerts, generally need to\u00a0open a network connection to make sure that FCM delivers the message to the\u00a0device without any delay. Set messages to high priority only when your\u00a0message is time-critical and it requires immediate response from the user.\u00a0However, keep in ind that setting your messages to high priority contributes\u00a0more to battery consumption compared with normal priority messages.<\/p>\n<p><strong>Limitations of Firebase Notifications :<\/strong><\/p>\n<p>Handling of\u00a0messages when app is in the background.<\/p>\n<p>There are two types of messages in FCM :<\/p>\n<p><strong>1. notification messages = display-messages :<\/strong><\/p>\n<p>These messages are displayed automatically <strong>when the app is in\u00a0background<\/strong>. These are delivered to the notification tray . When the user\u00a0taps on the notification, it opens up the app launcher, which is default\u00a0process. This also applies for messages that contain both notification and data\u00a0payload. However,\u00a0If the app is already in foreground only then\u00a0<em>FirebaseMessagingService.onMessageReceived()<\/em> is called.<\/p>\n<p><strong>2. data messages (custom key-value pairs):<\/strong><\/p>\n<p>These messages are also <strong>automatically displayed when the app is in\u00a0background<\/strong> and FirebaseMessagingService.onMessageReceived()is not\u00a0invoked. These are delivered to the notification tray .\u00a0If the app is already in foreground, only then FirebaseMessagingService.onMessageReceived() is called. In this case,<strong> data\u00a0message are received in onMessageReceived()<\/strong> and client can handle\u00a0the key-value pairs accordingly.<\/p>\n<p>Firebase team have developed a UI to send<strong> data-messages<\/strong> to the devices.\u00a0However, this data could not be received at your end. Hence, firebase console\u00a0should not be used for data-messages.<\/p>\n<p>So, if you want to process the messages arrived in the background, you have\u00a0to send <strong>&#8216;click_action&#8217;<\/strong> with message.<strong> click_action<\/strong> is a parameter of the\u00a0notification payload. However, it is not possible while sending messages from\u00a0Firebase console, but only via Firebase API.<strong>\u00a0click_action<\/strong> indicates the action associated when the user clicks on the\u00a0notification.\u00a0If this is defined, then the activity with a matching intent filter is launched\u00a0when user clicks the notification.<\/p>\n<p>How to achieve this ?<\/p>\n<p style=\"text-align: left;\"><strong>1.<\/strong> Go to your AndroidManifest.xml. Go to the activity you want to open on<br \/>\nclick of notification. Set its intent filter as below &#8211;<br \/>\n<strong>&lt;intent-filter&gt;<\/strong><br \/>\n<strong> &lt;action android:name=&#8221;com.ttnd.fcmtest.OPEN_ACTIVITY_1&#8243; \/&gt;<\/strong><br \/>\n<strong> &lt;category<\/strong><br \/>\n<strong> android:name=&#8221;android.intent.category.DEFAULT&#8221; \/&gt;<\/strong><br \/>\n<strong> &lt;\/intent-filter&gt;<\/strong><br \/>\n<strong>2.<\/strong> Get a RESTClient &#8211; <a title=\"RESTClient\" href=\"http:\/\/restclient.net\" target=\"_blank\">RESTClient<\/a><br \/>\n<strong>3.<\/strong> Send an HTTP POST Request to the URL mentioned below :<br \/>\n<a href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/http-server-ref\" target=\"_blank\">Firebase Cloud Messaging HTTP Protocol<\/a><br \/>\n<strong>4.<\/strong> Set Headers &#8211; <em>Key : Content-Type, Value : application\/json<\/em><br \/>\n<em>Key : Authorization, Value: key=&lt; your-server-key &gt;<\/em><\/p>\n<p style=\"text-align: left;\"><strong>5.<\/strong> Body :<\/p>\n<p><em> {<\/em><br \/>\n<em> &#8220;to&#8221;: &#8220;&lt; registration token&gt;&#8221;,<\/em><br \/>\n<em> &#8220;notification&#8221;: {&#8220;body&#8221;: &#8220;Exciting match!&#8221;,<\/em><br \/>\n<em> &#8220;title&#8221;: &#8220;India vs. Pakistan&#8221;,<\/em><br \/>\n<em> &#8220;click_action&#8221;: &#8220;com.apurv.fcmtest.OPEN_ACTIVITY_1&#8221;<\/em><br \/>\n<em> },<\/em><br \/>\n<em> &#8220;data&#8221;: {<\/em><br \/>\n<em> &#8220;custom_key&#8221;: &#8220;custom_value&#8221;,<\/em><br \/>\n<em> &#8220;Match&#8221;: &#8220;IndiaVSPakistan&#8221;<\/em><br \/>\n<em> }<\/em><br \/>\n<em> }<\/em><\/p>\n<p><strong>6.<\/strong> Click on Send Button. You&#8217;ll receive message with data payload.<br \/>\nYou can access this data in the<em> onCreate()<\/em> of your respective Activity.<\/p>\n<p>Voil\u00e0&#8230;..you are done!!<\/p>\n<p>Enjoy FCM.<\/p>\n<p>Below are some useful links for various operations :<\/p>\n<ul>\n<li>How to upgrade from GCM SDK to FCM SDK?<br \/>\n<a href=\"https:\/\/developers.google.com\/cloud-messaging\/android\/android-migrate-fcm\" target=\"_blank\">for Android<\/a><br \/>\n<a href=\"https:\/\/developers.google.com\/cloud-messaging\/ios\/ios-migrate-fcm\" target=\"_blank\">for iOS<\/a><\/li>\n<li><a title=\"All about FCM message types\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/concept-options\" target=\"_blank\">All about FCM message types<\/a><\/li>\n<li><a href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/server\" target=\"_blank\">About Firebase cloud messaging server<\/a><\/li>\n<li><a href=\"https:\/\/developers.google.com\/cloud-messaging\/downstream\" target=\"_blank\">DownStream Messages<\/a><\/li>\n<li><a href=\"https:\/\/developers.google.com\/cloud-messaging\/upstream\" target=\"_blank\">UpStream Messages<\/a><\/li>\n<li><a href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/android\/device-group\" target=\"_blank\">Device Group Messaging<\/a><\/li>\n<li><a href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/android\/topic-messaging\" target=\"_blank\">Topic Messages<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assuming that you are familiar with Google Cloud Messaging, the\u00a0new word for you in the title \u201cFirebaseCloudMessaging\u201d\u00a0or &#8220;FCM&#8221; is\u00a0Firebase. In case you don&#8217;t know about Firebase, let me give you a brief idea\u00a0about it.\u00a0Firebase is a cloud service provider, provided by Google, which\u00a0provides Mobile backend as a service (MBaaS), also known as\u00a0&#8220;backend as a service&#8221; [&hellip;]<\/p>\n","protected":false},"author":316,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":178},"categories":[518],"tags":[3902,3904,3903,3905],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39183"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/316"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=39183"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39183\/revisions"}],"predecessor-version":[{"id":51767,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39183\/revisions\/51767"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=39183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=39183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=39183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}