Introduction
Integrating push notification segmentation elevates user engagement by delivering targeted messages based on user preferences, behavior, or demographics. OneSignal is a popular, cross-platform push notification service that simplifies segmentation. In this tutorial, you’ll learn how to integrate OneSignal with a Flutter app, define user segments via tags, send segmented messages from the dashboard or via REST API, and handle notifications in your Flutter code.
Setup OneSignal SDK in Flutter
To begin, add the OneSignal Flutter SDK package to your pubspec.yaml dependencies:
dependencies:
flutter:
sdk: flutter
onesignal_flutter
Run flutter pub get to install the package. Then configure your AndroidManifest.xml and Info.plist with your OneSignal App ID. Follow OneSignal’s official guide to add the required permissions and metadata keys.
For Android (AndroidManifest.xml inside ):
<meta-data android:name="onesignal_app_id"
android:value="YOUR_ONESIGNAL_APP_ID"
For iOS (Info.plist):
<key>OneSignal_APPID</key>
<string>YOUR_ONESIGNAL_APP_ID</string
Initialize OneSignal and Request Permissions
In your main.dart, initialize OneSignal as soon as the app starts. This ensures you capture the device state, prompt the user for permissions, and assign a unique player ID.
import 'package:flutter/material.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
OneSignal.shared.setAppId('YOUR_ONESIGNAL_APP_ID');
OneSignal.shared.promptUserForPushNotificationPermission();
runApp(MyApp());
}With this initialization, OneSignal automatically retrieves the device’s push token and player ID, storing it on their servers.
Tagging Users and Defining Segments
Segmentation relies on tags: key-value pairs attached to user profiles. Tag wealthy customers as {"vip":"true"}, gamers as {"interest":"gaming"}, or assign geographical properties.
Example of tagging in Flutter:
void setUserTags() async {
await OneSignal.shared.sendTags({
'vip': 'true',
'region': 'europe'
});
}You can update or remove tags using sendTags() or deleteTag(). Combine multiple tags to build complex segments in the OneSignal Dashboard:
• vip users in Europe
• first-time app openers in Asia
• users who completed a purchase
Sending Segmented Push Notifications
You can send segmented notifications from the OneSignal Dashboard by selecting Audience > Filter by Tag. Alternatively, use OneSignal’s REST API for programmatic access.
Example API payload for a segment based on tags:
POST https://onesignal.com/api/v1/notifications
{
"app_id": "YOUR_ONESIGNAL_APP_ID",
"filters": [
{"field": "tag", "key": "vip", "relation": "=", "value": "true"},
{"operator": "AND"},
{"field": "tag", "key": "region", "relation": "=", "value": "europe"}
],
"headings": {"en": "Exclusive Offer!"},
"contents": {"en": "Dear VIP, enjoy 20% off on your next purchase."
This filter targets only users tagged as VIPs in Europe. You can combine tags with operators to refine your audience.
Handling Notification Open and Receive Events
Capturing events lets you react to custom payloads or deep links. Use event handlers in Flutter:
void configureNotificationHandlers() {
OneSignal.shared.setNotificationWillShowInForegroundHandler((event) {
event.complete(event.notification);
});
OneSignal.shared.setNotificationOpenedHandler((openedResult) {
var data = openedResult.notification.additionalData;
if (data != null && data['screen'] == 'promo') {
Navigator.pushNamed(context, '/promo');
}
});
}Register these handlers after initialization. They ensure you can navigate users, log analytics, or update UI based on notification content.
Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.
Conclusion
By integrating OneSignal with Flutter, you streamline push notification segmentation, delivering personalized messages that improve engagement and retention. You’ve seen how to install the SDK, initialize it, tag users, send segmented notifications via dashboard or REST API, and handle notification events. Apply these steps to craft targeted campaigns and boost user satisfaction in your Flutter app.