How To Send Notification using OneSignal with Flutter and Firebase.

Send the Notification From One Device to another Device using the OneSignal Rest API.

Sanjay As
4 min readApr 23, 2021

This is common knowledge that notifications are one of the most important communication channels between users and apps. By using this as a means of communication one can reach out to the user with some short messages and can interact with them. Push notifications are the best means to notify the updates to the existing users.

Out of the many services available to integrate push notifications into your flutter applications, we are going to use the OneSignal service in this article. You will learn how to add the push notifications feature and to send the push notification from device to device in Flutter using OneSignal.

Tested On Android Devices.

Advantages Of OneSignal

  1. Set up push notifications in OneSignal is simple and easy to follow.
  2. Supports unlimited devices and notifications without any charges.
  3. It provides a Single API and UI to send the notification across different platforms like Android, IOS, and Web.
  4. It is easy to connect to the Firebase Console.

Getting Started With the OneSignal

Step 1: App Registration with OneSignal.

  • Register an account at OneSignal.
  • Click on Add a new app. Select the android and Fill in the name of the app and click configure your platform.
Enter the name of Your app and Select the platform
  • Click On Configure your Platform.

Step 2: Add the firebase server Key and sender id.

  • To find the firebase server key and the sender id go to your firebase console.
  • open the project->project setting and select the Cloud Messaging. Then you find the server key and sender Id.
copy the server key and sender ID
  • Now fill in the server key and the sender id and click on the Save & Continue.
paste the server key, sender Id, and Save.
  • Then Select the platform as Flutter and continue. then Click on Done.
  • Now in the OneSignal website navigate to the Settings->Keys & IDs to find your App Id and Rest API Key
find your app id and rest API key.

step 3: Integrate the one signal to your Flutter App.

  • Add the one signal Plugin to the pubspec.yaml file and run Pub get.
  • Navigate to android->app->build. gradle and add the following lines of code.
Add the above line in the build. gradle
  • Initialize the one signal by adding the following line of code in the init state of the main. dart file of your project.
add this block of code in the main. dart file
  • Build your App Now, This would register a user on the one signal.
  • After building the flutter application check the dashBoard on the OneSignal website to view the subscribed users.
You can see the number of active and registered users of your application.

Step 5: To send the notification from one device to another.

  • To send the notification you need the token id of the user to whom you want to send the notification.
  • To get the Token Id the add the following line of code and add the token id into the database of the user.
  • While creating a new User add the user Token Id as one of the fields in the dataBase.
  • To make the API Call the HTTP plugin must be added to the pubspec.yaml
  • To send the Notification Use the following lines of code.
  • In the Above Code, kAppId is the App Id That You get from the OneSignal website and tokenIdList is the list of all the token IDs to Whom notification must be sent.

Demo

Conclusion

As the push notification is such an important feature of the mobile application we will come up with a demonstration video ASAP to integrate the notification into the flutter applications.

I hope that you guys find the article useful to integrate the push notifications into your flutter application. If you have any questions or suggestions, please feel free to comment here. Thank you very much!

--

--

Sanjay As

I am an avid learner with holistic knowledge of software development and design. I make apps for Android and IOS