Push Notifications in Flutter Using OneSignal

Push notifications are an important part of modern mobile development, and can be used to keep users engaged and informed about events in your app. In this article, we'll look at how to send push notifications in Flutter using OneSignal, a popular third-party service.

flutter push notifications onesignal

Prerequisites

Before getting started, you'll need to have the following installed on your machine:

  • Flutter SDK
  • An IDE (Android Studio, Visual Studio Code, etc.)
  • OneSignal account

Creating a Flutter Project

Once you've installed the prerequisites, you can create a new Flutter project. Open up your terminal and type the following command:

flutter create <project_name>

Replace <project_name> with the name of your project. Once the project has been created, you can open it in your IDE of choice.

Installing OneSignal Dependencies

We'll be using the onesignal_flutter package to send push notifications in Flutter. To install it, open up the pubspec.yaml file and add the following line:

dependencies:
  onesignal_flutter: ^1.0.9

Once you've added the dependency, you can run the following command in the terminal to install it:

flutter packages get

Sending Push Notifications in Flutter Using OneSignal

Now that we have all of the necessary dependencies, we can start sending push notifications in Flutter using OneSignal.

First, we'll need to configure our OneSignal account. To do this, head over to the OneSignal dashboard and create an account. Once you've signed up, you'll need to create a new app and add a platform. Choose Android as the platform and fill in the necessary details.

Next, we'll need to add the OneSignal SDK to our Flutter project. To do this, open up the AndroidManifest.xml file and add the following lines:

<!-- OneSignal -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<application>
  <!-- OneSignal -->
  <meta-data android:name="onesignal_app_id" android:value="<ONESIGNAL_APP_ID>" />
  <meta-data android:name="onesignal_google_project_number" android:value="<ONESIGNAL_GOOGLE_PROJECT_NUMBER>" />
</application>

Replace <ONESIGNAL_APP_ID> and <ONESIGNAL_GOOGLE_PROJECT_NUMBER> with your OneSignal app ID and Google project number respectively.

Now, we can start sending push notifications in Flutter using OneSignal. To do this, we'll need to initialize the OneSignal SDK in our main.dart file:

import 'package:onesignal_flutter/onesignal_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await OneSignal.shared.init("<ONESIGNAL_APP_ID>");
  runApp(MyApp());
}

Replace <ONESIGNAL_APP_ID> with your OneSignal app ID.

Finally, we can send push notifications in Flutter using OneSignal. To do this, we'll need to call the sendNotification method:

await OneSignal.shared.sendNotification(
  OSNotification(
    title: 'Hello World!',
    body: 'This is a test notification',
    contentAvailable: true
  )
);

This will send a push notification with the title Hello World! and the body This is a test notification.

Conclusion

In this article, we looked at how to send push notifications in Flutter using OneSignal. We installed the necessary dependencies, configured our OneSignal account, and sent a test notification. With the help of OneSignal, you can easily send push notifications in Flutter and keep users engaged and informed.