Site icon C1CTech

Android Firebase Cloud Messaging Example

<h3><span id&equals;"What-is-Firebase-Cloud-Messaging" style&equals;"color&colon; &num;000080&semi;">What is Firebase Cloud Messaging&quest;<&sol;span><&sol;h3>&NewLine;<p>It is a service provided by Google&period; Google says  &OpenCurlyDoubleQuote;<strong><span style&equals;"color&colon; &num;0000ff&semi;">Firebase Cloud Messaging<&sol;span><&sol;strong> &lpar;FCM&rpar; is a cross-platform messaging solution that lets you reliably deliver messages at no cost&period;”<&sol;p>&NewLine;<p>So it is free and at the same time easy to use&period; Previously we were using Google Cloud Messaging&comma; but nowadays GCM is obsolete&period; So you should use firebase only&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4><span id&equals;"Message-Types" style&equals;"color&colon; &num;000080&semi;">Message Types<&sol;span><&sol;h4>&NewLine;<p>Before moving ahead in the post let’s first understand the types of message that can be sent using FCM&period;<&sol;p>&NewLine;<ol>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Notification Message&colon;<&sol;span> <&sol;strong>Notification messages are handled by firebase SDK itself&period; Typically the notification message contains title&comma; message&comma; icon etc&period;&comma; These message can be sent from <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;console&period;firebase&period;google&period;com&sol;" target&equals;"&lowbar;blank" rel&equals;"nofollow noopener">firebase console<&sol;a> <&sol;span>UI&period; <&sol;strong>By sending this kind of messages&comma; you won’t get much control over the notification&period;This type of message is automatically displayed to end user&period; In this kind of message&comma; we have a predefined set of key-value pairs&period; We also have a data payload by using it we can set the custom key-value pairs as well&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Data message&colon;<&sol;strong><&sol;span> Default does not display this type of message to the end user&period; To show it we need to add some coding&period; This message contains only custom key-value pairs&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h3><span id&equals;"Adding-Firebase-Cloud-Messaging-to-Android-Project" style&equals;"color&colon; &num;000080&semi;">Adding Firebase Cloud Messaging to Android Project<&sol;span><&sol;h3>&NewLine;<h4><span id&equals;"Creating-a-new-Android-Studio-Project" style&equals;"color&colon; &num;0000ff&semi;">Creating a new Android Studio Project<&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li>I have created a blank project using an Empty Activity&comma; and I named it <span style&equals;"color&colon; &num;008000&semi;"><strong>FirebasePushNotificationExample<&sol;strong>&period;<&sol;span><&sol;li>&NewLine;<li>So you have to do the same&comma; you can change the project name to whatever you want&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span id&equals;"Adding-Firebase-Cloud-Messaging" style&equals;"color&colon; &num;0000ff&semi;">Adding Firebase Cloud Messaging<&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li>Click on tools and then select Firebase&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"size-medium wp-image-464 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;select&lowbar;firebase&lowbar;ref-287x300&period;png" alt&equals;"" width&equals;"287" height&equals;"300" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>It will open an Assistant Window on the Right&period; From here you need to Select <strong><span style&equals;"color&colon; &num;008000&semi;">Cloud Messaging<&sol;span> <&sol;strong>&lpar;As shown in the image&rpar;&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter wp-image-463 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;select&lowbar;fcm&lowbar;ref&period;png" alt&equals;"" width&equals;"598" height&equals;"716" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>Here you need to do two things&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span id&equals;"Connect-to-Firebase">Connect to Firebase<&sol;span><&sol;strong><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>Click on the first Button&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>Connect to Firebase<&sol;strong>&period;<&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter wp-image-465 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Capture103&period;png" alt&equals;"" width&equals;"601" height&equals;"314" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>It will open a new Window&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"wp-image-466 size-full aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Capture104&period;png" alt&equals;"" width&equals;"794" height&equals;"659" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>From here you can create a new Firebase Project&comma; or you can also select your existing project on firebase if any&period; Here we are building a new Firebase Project&period; Just put the name that you want for your project and click on <span style&equals;"color&colon; &num;008000&semi;"><strong>Connect to Firebase<&sol;strong>&period;<&sol;span><&sol;li>&NewLine;<li>Now wait for a while&comma; and you will see <span style&equals;"color&colon; &num;008000&semi;"><strong>Green Connected Message<&sol;strong>&period;<&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter wp-image-467 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Capture101&period;png" alt&equals;"" width&equals;"597" height&equals;"316" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span id&equals;"Add-FCM-to-your-app">Add FCM to your app<&sol;span><&sol;strong><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>Now click on the second button&period; Add FCM to your App&period; It will again open a new window&comma; and here you need to accept the changes&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-468 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Capture105&period;png" alt&equals;"" width&equals;"625" height&equals;"509" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>Then it will automatically do everything required for adding FCM to your application&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter wp-image-469 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Capture102&period;png" alt&equals;"" width&equals;"605" height&equals;"413" &sol;><&sol;p>&NewLine;<h3><span id&equals;"Ways-of-Receiving-Push-Notification" style&equals;"color&colon; &num;000080&semi;">Ways of Receiving Push Notification<&sol;span><&sol;h3>&NewLine;<p>Now first&comma; we need to understand how we receive or send notifications using FCM&period; So there are two ways&period;<&sol;p>&NewLine;<ol>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Using FCM Token&colon;<&sol;span> <&sol;strong>We use this method when we want to send a notification to a specific device&period; Or some dynamic group of devices&period; Upon initial startup&comma; the firebase SDK generates a registration token for the application&period; We use this token to identify the device&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Using Topic&colon;<&sol;span> <&sol;strong>We can create topics and let our users subscribe to those topics&period; Then we can send the message to the topic&period; And the message will be sent to all the users of that particular topic&period; In this method&comma; we don’t need to store any token&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong><span style&equals;"color&colon; &num;993366&semi;">We can select from the above two methods to implement in our application&period; Here we will learn about the FCM Token<&sol;span> &period;<&sol;strong><&sol;span><&sol;p>&NewLine;<h3><span id&equals;"Firebase-Cloud-Messaging-using-FCM-Access-Token" style&equals;"color&colon; &num;000080&semi;">Firebase Cloud Messaging using FCM Access Token<&sol;span><&sol;h3>&NewLine;<p>In this method first&comma; we will generate the access token&period; Now to get the access token&comma; we create a class that extends <span style&equals;"color&colon; &num;008000&semi;"><strong>FirebaseInstanceIdService<&sol;strong>&period;<&sol;span> It is a service that we also need to define inside our Manifest file&period; Now let’s see how we can do this in our project&period;<&sol;p>&NewLine;<h4><span id&equals;"Generating-Access-Token" style&equals;"color&colon; &num;0000ff&semi;">Generating Access Token<&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li>Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>MyFirebaseInstanceIdService<&sol;strong><&sol;span> in your project&comma; and write the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyFirebaseInstanceIdService&period;Java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;firebasepushnotificationexample&semi;&NewLine;&NewLine;import android&period;util&period;Log&semi;&NewLine;import com&period;google&period;firebase&period;iid&period;FirebaseInstanceId&semi;&NewLine;import com&period;google&period;firebase&period;iid&period;FirebaseInstanceIdService&semi;&NewLine;&NewLine;&NewLine;public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;this method will be called&NewLine; &sol;&sol;when the token is generated<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onTokenRefresh&lpar;&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Get updated InstanceID token&period;<&sol;strong><&sol;span>&NewLine; String refreshedToken &equals; FirebaseInstanceId&period;getInstance&lpar;&rpar;&period;getToken&lpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;for now we are displaying the token in the log&NewLine; &sol;&sol;copy it as this method is called only when the new token is generated&NewLine; &sol;&sol;and usually new token is only generated when the app is reinstalled or the data is cleared<&sol;strong><&sol;span>&NewLine;&NewLine; Log&period;d&lpar;"Refreshed token&colon; "&comma; refreshedToken&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<div id&equals;"crayon-5b19054a88959248495628" class&equals;"crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings&equals;" no-popup minimize scroll-mouseover">&NewLine;<div class&equals;"crayon-toolbar" data-settings&equals;" show">&NewLine;<ul>&NewLine;<li>As this class is a service&comma; we need to define it in <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> as well&period;<&sol;li>&NewLine;<li>So open your <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span>file and just before the closing <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&sol;application&gt&semi;<&sol;strong><&sol;span> tag add the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code>&lt&semi;service&NewLine; android&colon;name&equals;"&period;MyFirebaseInstanceIdService"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"com&period;google&period;firebase&period;INSTANCE&lowbar;ID&lowbar;EVENT"&sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine;&lt&semi;&sol;service&gt&semi;<&sol;code><&sol;pre>&NewLine;<h4><span id&equals;"Receiving-Messages" style&equals;"color&colon; &num;0000ff&semi;">Receiving Messages<&sol;span><&sol;h4>&NewLine;<p>To receive the message we need to create a class that will extend <span style&equals;"color&colon; &num;008000&semi;"><strong>FirebaseMessagingService<&sol;strong><&sol;span>&period; Here we will override a method <strong><span style&equals;"color&colon; &num;008000&semi;">onMessageReceived<&sol;span>&lpar;&rpar;<&sol;strong> that is called when we receive a message&period; Again this is also a service&comma; so we need to define it in our <span style&equals;"color&colon; &num;008000&semi;"><strong>AndoirdManifest&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul>&NewLine;<li>Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>MyFirebaseMessagingService<&sol;strong><&sol;span> and write the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyFirebaseMessagingService&period;Java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;firebasepushnotificationexample&semi;&NewLine;&NewLine;import android&period;util&period;Log&semi;&NewLine;&NewLine;import com&period;google&period;firebase&period;messaging&period;FirebaseMessagingService&semi;&NewLine;import com&period;google&period;firebase&period;messaging&period;RemoteMessage&semi;&NewLine;&NewLine;import static android&period;content&period;ContentValues&period;TAG&semi;&NewLine;&NewLine;public class MyFirebaseMessagingService extends FirebaseMessagingService &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; public void onMessageReceived&lpar;RemoteMessage remoteMessage&rpar; &lbrace;&NewLine; super&period;onMessageReceived&lpar;remoteMessage&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Check if message contains a data payload&period;<&sol;span><&sol;strong>&NewLine; if &lpar;remoteMessage&period;getData&lpar;&rpar;&period;size&lpar;&rpar; &gt&semi; 0&rpar; &lbrace;&NewLine; &NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;handle the data message here<&sol;span><&sol;strong>&NewLine; Log&period;d&lpar;TAG&comma; "Message data payload&colon; " &plus; remoteMessage&period;getData&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;getting the title and the body<&sol;span><&sol;strong>&NewLine; String title &equals; remoteMessage&period;getNotification&lpar;&rpar;&period;getTitle&lpar;&rpar;&semi;&NewLine; String body &equals; remoteMessage&period;getNotification&lpar;&rpar;&period;getBody&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>Now again define it using the below XML code in your <strong><span style&equals;"color&colon; &num;008000&semi;">AndroidManifest&period;xml<&sol;span><&sol;strong> just before the closing tag <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&sol;application<&sol;strong>&gt&semi;&period;<&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code>&lt&semi;service&NewLine; android&colon;name&equals;"&period;MyFirebaseMessagingService"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"com&period;google&period;firebase&period;MESSAGING&lowbar;EVENT"&sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine;&lt&semi;&sol;service&gt&semi;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div id&equals;"crayon-5b19054a88972495550349" class&equals;"crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings&equals;" no-popup minimize scroll-mouseover">&NewLine;<div class&equals;"crayon-toolbar" data-settings&equals;" show">&NewLine;<h3><span id&equals;"Building-Push-Notification" style&equals;"color&colon; &num;000080&semi;">Building Push Notification<&sol;span><&sol;h3>&NewLine;<p>Now let’s build the Push Notification&period; For this&comma; I will use a Singleton class&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;993366&semi;">Note&colon; From Android Oreo&comma; you need to create a notification channel or else notification will not be displayed&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>So first we will define some constants for our Notification Channel in a separate class&period; Create a class named <strong><span style&equals;"color&colon; &num;008000&semi;">Constants<&sol;span><&sol;strong>&period; And write the following code<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<pre><code>package com&period;c1ctech&period;firebasepushnotificationexample&semi;&NewLine;&NewLine;public class Constants &lbrace;&NewLine;&NewLine; public static final String CHANNEL&lowbar;ID &equals; "my&lowbar;channel&lowbar;01"&semi;&NewLine; public static final String CHANNEL&lowbar;NAME &equals; "Simplified Coding Notification"&semi;&NewLine; public static final String CHANNEL&lowbar;DESCRIPTION &equals; "www&period;simplifiedcoding&period;net"&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>Now&comma; create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>MyNotificationManager<&sol;strong> <&sol;span>and write the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MyNotificationManager&period;Java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;firebasepushnotificationexample&semi;&NewLine;&NewLine;import android&period;app&period;NotificationManager&semi;&NewLine;import android&period;app&period;PendingIntent&semi;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;content&period;Intent&semi;&NewLine;import android&period;support&period;v4&period;app&period;NotificationCompat&semi;&NewLine;&NewLine;import static android&period;content&period;Context&period;NOTIFICATION&lowbar;SERVICE&semi;&NewLine;&NewLine;&NewLine;public class MyNotificationManager &lbrace;&NewLine;&NewLine; private Context mCtx&semi;&NewLine; private static MyNotificationManager mInstance&semi;&NewLine;&NewLine; private MyNotificationManager&lpar;Context context&rpar; &lbrace;&NewLine; mCtx &equals; context&semi;&NewLine; &rcub;&NewLine;&NewLine; public static synchronized MyNotificationManager getInstance&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;mInstance &equals;&equals; null&rpar; &lbrace;&NewLine; mInstance &equals; new MyNotificationManager&lpar;context&rpar;&semi;&NewLine; &rcub;&NewLine; return mInstance&semi;&NewLine; &rcub;&NewLine;&NewLine; public void displayNotification&lpar;String title&comma; String body&rpar; &lbrace;&NewLine;&NewLine; NotificationCompat&period;Builder mBuilder &equals;&NewLine; new NotificationCompat&period;Builder&lpar;mCtx&comma; Constants&period;CHANNEL&lowbar;ID&rpar;&NewLine; &period;setSmallIcon&lpar;R&period;drawable&period;app&lowbar;icon&rpar;&NewLine; &period;setContentTitle&lpar;title&rpar;&NewLine; &period;setContentText&lpar;body&rpar;&semi;&NewLine;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&ast;&NewLine; &ast; Clicking on the notification will take us to this intent&NewLine; &ast; Right now we are using the MainActivity as this is the only activity we have in our application&NewLine; &ast; But for your project you can customize it as you want&NewLine; &ast; &ast;&sol;<&sol;strong><&sol;span>&NewLine;&NewLine; Intent resultIntent &equals; new Intent&lpar;mCtx&comma; MainActivity&period;class&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&ast;&NewLine; &ast; Now we will create a pending intent&NewLine; &ast; The method getActivity is taking 4 parameters&NewLine; &ast; All paramters are describing themselves&NewLine; &ast; 0 is the request code &lpar;the second parameter&rpar;&NewLine; &ast; We can detect this code in the activity that will open by this we can get&NewLine; &ast; Which notification opened the activity&NewLine; &ast; &ast;&sol;<&sol;span><&sol;strong>&NewLine; PendingIntent pendingIntent &equals; PendingIntent&period;getActivity&lpar;mCtx&comma; 0&comma; resultIntent&comma; PendingIntent&period;FLAG&lowbar;UPDATE&lowbar;CURRENT&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&ast;&NewLine; &ast; Setting the pending intent to notification builder&NewLine; &ast; &ast;&sol;<&sol;span><&sol;strong>&NewLine;&NewLine; mBuilder&period;setContentIntent&lpar;pendingIntent&rpar;&semi;&NewLine;&NewLine; NotificationManager mNotifyMgr &equals;&NewLine; &lpar;NotificationManager&rpar; mCtx&period;getSystemService&lpar;NOTIFICATION&lowbar;SERVICE&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&ast;&NewLine; &ast; The first parameter is the notification id&NewLine; &ast; better don't give a literal here &lpar;right now we are giving a int literal&rpar;&NewLine; &ast; because using this id we can modify it later&NewLine; &ast; &ast;&sol;<&sol;span><&sol;strong>&NewLine; if &lpar;mNotifyMgr &excl;&equals; null&rpar; &lbrace;&NewLine; mNotifyMgr&period;notify&lpar;1&comma; mBuilder&period;build&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h3><span id&equals;"Testing-a-Local-Notification" style&equals;"color&colon; &num;000080&semi;">Testing a Local Notification<&sol;span><&sol;h3>&NewLine;<p>Before moving ahead&comma; we will confirm that the notification is working correctly&period; For this&comma; we will create a local notification from the app itself&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Come on <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong> <&sol;span>and write the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;firebasepushnotificationexample&semi;&NewLine;&NewLine;import android&period;app&period;NotificationChannel&semi;&NewLine;import android&period;app&period;NotificationManager&semi;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;graphics&period;Color&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;If the device is having android oreo we will create a notification channel<&sol;span><&sol;strong>&NewLine;&NewLine; if &lpar;android&period;os&period;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; android&period;os&period;Build&period;VERSION&lowbar;CODES&period;O&rpar; &lbrace;&NewLine; NotificationManager mNotificationManager &equals;&NewLine; &lpar;NotificationManager&rpar; getSystemService&lpar;Context&period;NOTIFICATION&lowbar;SERVICE&rpar;&semi;&NewLine; int importance &equals; NotificationManager&period;IMPORTANCE&lowbar;HIGH&semi;&NewLine; NotificationChannel mChannel &equals; new NotificationChannel&lpar;Constants&period;CHANNEL&lowbar;ID&comma; Constants&period;CHANNEL&lowbar;NAME&comma; importance&rpar;&semi;&NewLine; mChannel&period;setDescription&lpar;Constants&period;CHANNEL&lowbar;DESCRIPTION&rpar;&semi;&NewLine; mChannel&period;enableLights&lpar;true&rpar;&semi;&NewLine; mChannel&period;setLightColor&lpar;Color&period;RED&rpar;&semi;&NewLine; mChannel&period;enableVibration&lpar;true&rpar;&semi;&NewLine; mChannel&period;setVibrationPattern&lpar;new long&lbrack;&rsqb;&lbrace;100&comma; 200&comma; 300&comma; 400&comma; 500&comma; 400&comma; 300&comma; 200&comma; 400&rcub;&rpar;&semi;&NewLine; mNotificationManager&period;createNotificationChannel&lpar;mChannel&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Displaying a notification locally<&sol;span><&sol;strong>&NewLine; MyNotificationManager&period;getInstance&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;displayNotification&lpar;"Hi"&comma;"Where are you&quest;"&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>Now try running your application&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><img class&equals;"wp-image-477 size-full aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Screenshot&lowbar;2018-06-09-16-41-441&period;png" alt&equals;"" width&equals;"480" height&equals;"800" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span id&equals;"Testing-Notification-from-Firebase-Console" style&equals;"color&colon; &num;000080&semi;">Testing Notification from Firebase Console<&sol;span><&sol;h3>&NewLine;<ul>&NewLine;<li>Now let’s send a real notification from Firebase Console&period;<&sol;li>&NewLine;<li>Open firebase console&comma; then open your project that you are using&period; Then from the left menu click on <strong><span style&equals;"color&colon; &num;008000&semi;">grow -&gt&semi; Cloud Messaging&period;<&sol;span> <&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-470 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;sendmessage&period;png" alt&equals;"" width&equals;"1173" height&equals;"845" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>And after pressing the <span style&equals;"color&colon; &num;008000&semi;"><strong>Send Message<&sol;strong><&sol;span> button&comma; you should see your message on the application&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-481 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Screenshot&lowbar;2018-06-08-17-53-081&period;png" alt&equals;"" width&equals;"480" height&equals;"800" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version