Integrating Push Notification Segmentation with OneSignal in Flutter

Summary
Summary
Summary
Summary

This tutorial guides you through integrating OneSignal in a Flutter app for push notification segmentation. Learn to install the SDK, initialize OneSignal, apply user tags, define segments, send notifications from the dashboard or via API, and handle notification events to create personalized campaigns.

This tutorial guides you through integrating OneSignal in a Flutter app for push notification segmentation. Learn to install the SDK, initialize OneSignal, apply user tags, define segments, send notifications from the dashboard or via API, and handle notification events to create personalized campaigns.

This tutorial guides you through integrating OneSignal in a Flutter app for push notification segmentation. Learn to install the SDK, initialize OneSignal, apply user tags, define segments, send notifications from the dashboard or via API, and handle notification events to create personalized campaigns.

This tutorial guides you through integrating OneSignal in a Flutter app for push notification segmentation. Learn to install the SDK, initialize OneSignal, apply user tags, define segments, send notifications from the dashboard or via API, and handle notification events to create personalized campaigns.

Key insights:
Key insights:
Key insights:
Key insights:
  • Setup OneSignal SDK in Flutter: Add the onesignal_flutter package, configure AndroidManifest and Info.plist with your App ID.

  • Initialize OneSignal and Request Permissions: Call setAppId and promptUserForPushNotificationPermission during app startup.

  • Tagging Users and Defining Segments: Use sendTags to attach key-value pairs, then build segments in the Dashboard based on tags.

  • Sending Segmented Push Notifications: Target specific segments by filters in the OneSignal Dashboard or via the REST API payload.

  • Handling Notification Open and Receive Events: Implement handlers to react to foreground and opened notifications for deep linking and analytics.

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) {
    // Display an in-app alert or bypass
    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.

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) {
    // Display an in-app alert or bypass
    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.

Build Flutter Apps Faster with Vibe Studio

Build Flutter Apps Faster with Vibe Studio

Build Flutter Apps Faster with Vibe Studio

Build Flutter Apps Faster with Vibe Studio

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Other Insights

Other Insights

Other Insights

Other Insights

Join a growing community of builders today

Join a growing community of builders today

Join a growing community of builders today

Join a growing community of builders today

Join a growing community of builders today

28-07 Jackson Ave

Walturn

New York NY 11101 United States

© Steve • All Rights Reserved 2025

28-07 Jackson Ave

Walturn

New York NY 11101 United States

© Steve • All Rights Reserved 2025

28-07 Jackson Ave

Walturn

New York NY 11101 United States

© Steve • All Rights Reserved 2025

28-07 Jackson Ave

Walturn

New York NY 11101 United States

© Steve • All Rights Reserved 2025

28-07 Jackson Ave

Walturn

New York NY 11101 United States

© Steve • All Rights Reserved 2025

28-07 Jackson Ave

Walturn

New York NY 11101 United States

© Steve • All Rights Reserved 2025