How To Send Notification using OneSignal with Flutter and Firebase.
Send the Notification From One Device to another Device using the OneSignal Rest API.
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
- Set up push notifications in OneSignal is simple and easy to follow.
- Supports unlimited devices and notifications without any charges.
- It provides a Single API and UI to send the notification across different platforms like Android, IOS, and Web.
- 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.
- 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.
- Now fill in the server key and the sender id and click on the Save & Continue.
- 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
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.
- Initialize the one signal by adding the following line of code in the init state of the main. dart file of your project.
- 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.
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.
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!