Sending Push notification with Flutter
Hello everyone in this blog I will show you how can you send push notifications from Firebase to your flutter app.
Let’s get started.
The only prerequisite for this is that you need to have a flutter app. If you don’t have one you can run the command given below to make a flutter app, the default one.
flutter create app_name
If you have worked with flutter then you might be knowing that the app_name has certain conditions that are to be followed. Remember them while naming your app.
Once you have the app created you now have to open the build.gradle file which is present inside the android folder as shown in the image below.
In this file add the classpath and subprojects code(given below)on their specific positions as they are shown in the image below. You might be thinking what do they do?
Classpath is used to add the dependencies to the project so that the app can know about them and then fetch them accordingly. The subproject on the other is used to deal with the exceptions or configurations which are there.
Your build.gradle file should look similar to the one given below.
Now you have to click on the app folder and open the build.gradle file inside that as shown in the image below. The Gradle file open is the one just below the src folder.
Add the code given below at the last of this file.
apply plugin: 'com.google.gms.google-services'
This will add the google-services plugin to the app.
Now open the flutter console in any of your favorite browsers.
In the console add a new project.
Name your project whatever you want and click continue.
You can turn off the analytics if you want. I kept it on and click continue.
For account choose a default account which is shown when you click select account and click on create a project.
It will take around a minute for the project t be created. Once the project is created you then have to click on continue and a page similar to the one given below will be shown
You have to click on the android icon in the image above and you will be taken to another screen as shown below.
For the android package name go to the AndroidManifest.xml file at the particular location as shown. android/app/src/main/AndroidManifest.xml
In this file on the top, you can find your package name.
For Debug signing certificate SHA-1 one your terminal and type the code given below and it will provide you with an SHA-1 key. For password either click enter or type android and click enter.
keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
You will then get an output similar to the one shown below.
Copy the id and paste it in the firebase page and then click on the register app.
In step 2 for this, you will be getting a google-services.json file. Download it and place it inside the app folder as shown.
You can skip step 3 and step 4 in the firebase page. You will then be taken to a page similar to this. (this is a similar project I made)
I have two apps here.
You then have to click on the cloud messaging from the left navigation.
Click on send your first message.
Fill in the details according to you and click on the Send test message.
Here you will be needing the token for the mobile on which you want to send the notification. For this, you have to run your main.dart file which will install the app in your device/emulator. In the debug console you will be shown with a token. Once the token is displayed close to the app and remove from the recent activity section.
Copy the token and paste in the token section and click on the test.
You will then be seeing a notification that will be popped in your mobile notification bar.
In this way, you can send push notification from firebase to your android app and could increase interactions with the user.
If you have any problem you can comment it down or mail me at firstname.lastname@example.org