Firebase Push Notification :
Firebase Push Notification is a common feature we observe now a days in every app like social media.
This functionality will let apps show the latest message also every new message on the device is shown in the form of Push Notifications. Android Tutorial on Google Firebase Push Notification will let you know how to implement them much easily.
Firebase Push notifications also makes your app interactive, by sending the latest info and let user’s know it even they are not currently using your app as these services run on the background.
When ever there is any update by change in the app service you app need to not make multiple api calls to check for the update, it can directly know from the data received through Firebase push notifications.
Google Firebase Push Notifications will let you implement push notifications instantly by registering your device and adding necessary settings which makes your app enabled with push notifications.
Firebase Push Notification Video :
Adding Firebase Push Notification to your project :
build:gradle(Module:App)
dependencies { implementation 'com.google.firebase:firebase-messaging:11.0.4' }
build.gradle(Project)
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files classpath 'com.google.gms:google-services:3.1.0' } } allprojects { repositories { google() jcenter() } }
To get SHA1 Key please refer this link — SHA1 Generating Process
Now add google-services.json file to your project by using this file firebase push notifications services can be implemented in your app not only push notifications even analytics, login, data storage, and many ore services can be implemented based on this json file.
We are not adding any element in the layout file.
MainActivity :
We will be initializing the app using firebase and fetch the token which can be used to receive firebase push notifications.Based on the token devices are identified.
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Refreshed token: " + FirebaseInstanceId.getInstance().getToken()); } }
FcmService :
Using the FCM service we can fetch the latest token and can use it to continue using Firebase Push Notifications also update it to the database.
public class FcmService extends FirebaseInstanceIdService{ @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); } }
AndroidManifest.xml :
Need to specify the service in manifest file to make it start working with firebase push notification.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="push.android.com.androidpushnotifications"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".FcmService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> </application> </manifest>
If you are having any query’s in this tutorial on Android Firebase Push Notification do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates.