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
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
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
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
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
- to specific devices, if you know their device IDs
- to groups of devices, if you know the registration IDs of the groups
- to topics, which are just keys that devices can subscribe to
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
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
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
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
Post a Comment