Skip to main content

Push notification with Flutter using FCM

Sending messages to an android app using Flutter with FCM is something quite easy to do, we are going to do set all the necessary things to be able to get the messages from Firebase and we are going to send messages using the rest API, in this case you can use your own backend server to send the messages, in this case I'm going to show you using insomnia, there are some other things to keep in mind, you can send the messages
  1. to specific devices, if you know their device IDs
  2. to groups of devices, if you know the registration IDs of the groups
  3. to topics, which are just keys that devices can subscribe to
You'll note that there is no way to send to all devices explicitly. You can build such functionality with each of these though, for example: by subscribing the app to a topic when it starts (e.g. /topics/all) or by keeping a list of all device IDs, and then sending the message to all of those.

we are going to test how to send messages based on the token generate by a device

now if you need to send to all users we are going to be testing it how to do it using a topic and subscribing users to all users, we are going to be doing both ways

and now if you need to send messages to many users using the token way, get the token of each user each time the user set up the app by the first time and send the messages to the whole list of users or tokens you've got, try sending them in amounts of 1000 users, after that wait some seconds to keep sending messages based on that amount of users

it is up to you the way you want to send the messages

 
the first thing we are going to set up is your Firebase project using Linux

1: go to https://console.firebase.google.com/ and create a new project
2: let's create the flutter application project, name it as you want
flutter create push_notifications

now, open the flutter project and go to android -> app ->main -> AndroidManigest.xml to get package name
it's name in this case is called com.example.push_notifications, let's copy that one,
3: Get started by adding Firebase to your appand let's paste the page name we copied before, now give it a name to the app, and we must now add and SHA-1, it says it is optional bu in reality it is needed, the manual says the way to do it is paste this command on your terminal

keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore

if it asks for a password try giving it android or your own admin password, if that comand in somehow does not work, then run run this one 

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

this command returns the SHA1, copy that SHA1, and let's paste on the web app we are creating



 click on register app, and now let's download the google-services.json and let's move it to the
/android/app folder




we are almost done, let's open the pubspec.yaml and let's got to the https://pub.dev/ and let's find firebase_messagin and let's get the latest version name, and let's paste it in the pubspec.yaml


now let's keep on with these steps



 now, let's open the main.dart file and under the _MyHomePageState let's paste 


FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();


@override
void initState() {
  super.initState();

    _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
      print('on message $message');
    },
    onResume: (Map<String, dynamic> message) {
      print('on resume $message');
    },
    onLaunch: (Map<String, dynamic> message) {
      print('on launch $message');
    },
  );
 
  _firebaseMessaging.getToken().then((token){
    print(token);
  });
}

let's run it, and let's test it, let's got to the firebase console -> Cloud Messaging -> Send your first message

give it a tittle, a description and in the last step in the key field let's add click_action and in the value let's add FLUTTER_NOTIFICATION_CLICK, like this


 now let's go back to the flutter console,

we can see, it starts without any error, and it generates a token 

I/flutter ( 2038): d-yfR_R9zKM:APA91bElKEz2rE3yLqBPxvdA5qx1JvqBfiADTkOe2fOOckaWIlKrfu6ppubvc3yRuw5sS1TTY-vqp5ZqKfREaailax7UrCiBZMhB-Xq8o2aeHFtFTH6sqMRobIGp72JxJw4-HjYeApbB


ok, let's keep that token anywhere so that, with that one is the test we are going to be doing to send to an individual device, let's go back to the web again, and let's send the message,

Note: in development mode some times it takes a long time to get messages from FCM, I have not researched about it so far, but what I have been able to prove is that sometimes take a long time, if you do a flutter build apk, and takes that .apk to a mobile it works immediately,

so after a while I got the message

now let's minimize the application and resend the message again in order to see the notification upwards



now let's, try it from insomnia

let's create a post, the URL is https://fcm.googleapis.com/fcm/send
send it as Json, and send


{
"to" : "d-yfR_R9zKM:APA91bElKEz2rE3yLqBPxvdA5qx1JvqBfiADTkOe2fOOckaWIlKrfu6ppubvc3yRuw5sS1TTY-vqp5ZqKfREaailax7UrCiBZMhB-Xq8o2aeHFtFTH6sqMRobIGp72JxJw4-HjYeApbB",
"notification" : {
"body" : "This is a test from Insomnia",
"title" : "New topic"
"content_available" : true,
"priority" : "high",
},
"data" : {
"body" : "This is a test from Insomnia",
"title" : "New topic"
"content_available" : true,
"priority" : "high",
"sound": "enabled",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}

replace the token by the one you copied
 no there is another thing must be done in the headers in insomnia, adding an Authorization and in there paste the value key = your ID

and you get your ID from the FIrebase console -> your Application name - >Cloud Messaging -> your server key


so Insomnia you should like this


 test it and see the message in action


Comments

Popular posts from this blog

How to deploy a VueJS App using Nginx on Ubuntu

There are thousands of blogs and websites out there explaining how to do a hello world and how to start with VueJS, but in this little post, I’m just going to be explaining how to do deploy a VueJs app after you have run the command through the CLI npm run build . So when you run the command npm run build a dist folder is created and that folder’s got the essential .js files to run our app, when we want to run our app on an Nginx server by default the vue-router is not going to work well so that Nginx does not come ready to work by default with a VueJs app This is basically for a Linux Ubuntu distribution if you’ve got any other Linux distribution just pay attention where is going to be set the www/html folder and the Nginx settings so that this is useful for any Linux distribution  Install and configure nginx sudo apt install nginx Double check to make sure the nginx service is running with command service nginx status, then open your browser and enter url

How to secure SpringBoot with SSL and Tomcat or Undertow

when we are going to take our applications to production mode, we must have an SSL certificate for the FrontEnd and   BackEnd too, in this case, our backend is a  SpringBoot which is using a tomcat embedded server. Terminology TLS vs SSL TLS is the successor to SSL. It is a protocol that ensures privacy between communicating applications. Unless otherwise stated, in this document consider TLS and SSL as interchangable. Certificate (cert) The public half of a public/private key pair with some additional metadata about who issued it etc. It may be freely given to anyone. Private Key A private key can verify that its corresponding certificate/public key was used to encrypt data. It is never given out publicly. Certificate Authority (CA) A company that issues digital certificates. For SSL/TLS certificates, there are a small number of providers (e.g. Symantec/Versign/Thawte, Comodo, GoDaddy, LetsEncrypt) whose certificates are included by most browsers and Operating System

How to do pagination SpringBoot with Jbctemplate, MySQL

We are going to be working on a topic which is a basic need when doing any app, and it is Pagination. let's get started creating a product table at https://mockaroo.com/ create table products ( id INT, name VARCHAR(50), code VARCHAR(50) ); insert into products (id, name, code) values (1, 'Hettinger-Goyette', '42549-680'); insert into products (id, name, code) values (2, 'Konopelski-Klein', '49527-724'); insert into products (id, name, code) values (3, 'Smitham, Kuhlman and Balistreri', '53238-003'); insert into products (id, name, code) values (4, 'Hettinger, Weissnat and Goodwin', '0143-9916'); insert into products (id, name, code) values (5, 'Rowe Inc', '42291-898'); insert into products (id, name, code) values (6, 'Ernser-Hauck', '10544-617'); insert into products (id, name, code) values (7, 'Maggio and Sons', '68788-9087'); insert into products (id, name,