Skip to content

Flutter SDK Basic integration guide

This section contains information on how to integrate the Pushwoosh Flutter SDK into your application.

To integrate the Pushwoosh Flutter SDK into your app, you will need the following:

Add the pushwoosh_flutter package to your pubspec.yaml file:

pubspec.yaml
dependencies:
flutter:
sdk: flutter
# Use the latest version from https://pub.dev/packages/pushwoosh_flutter
pushwoosh_flutter: ^[LATEST_VERSION]

Check the latest version on pub.dev.

Then, run the following command in your project’s root directory to install the dependency:

Terminal window
flutter pub get

Double check that the package is installed correctly:

Terminal window
flutter pub deps | grep pushwoosh_flutter
# Example output:
# ❯ flutter pub deps | grep pushwoosh_flutter
# └── pushwoosh_flutter 2.3.11

In the root component of your main.dart file:

  • Import the pushwoosh_flutter package.
  • Initialize the Pushwoosh SDK.
  • Call registerForPushNotifications() in your initialization logic to register for push notifications.
main.dart
import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
void main() async {
runApp(const MyApp());
Pushwoosh.initialize({
"app_id": "__YOUR_APP_ID__",
"sender_id": "__YOUR_FCM_SENDER_ID__"
});
Pushwoosh.getInstance.registerForPushNotifications();
}

Where:

  • __YOUR_APP_ID__ is the application code from the Pushwoosh Control Panel.
  • __YOUR_FCM_SENDER_ID__ is the Firebase project number from the Firebase Console.

To enable Push Notifications in your project, you need to add certain capabilities.

In the Signing & Capabilities section, add the following capabilities:

  • Push Notifications
  • Background Modes. After adding this capability, check the box for Remote notifications.

If you intend to use Time Sensitive Notifications (iOS 15+), also add the Time Sensitive Notifications capability.

In your Runner/Info.plist set the __PUSHWOOSH_DEVICE_API_TOKEN__ key to the Pushwoosh Device API Token:

info.plist
<key>Pushwoosh_API_TOKEN</key>
<string>__PUSHWOOSH_DEVICE_API_TOKEN__</string>

You must add a Notification Service Extension target to your project. This is essential for accurate delivery tracking and features like Rich Media on iOS.

Follow the native guide’s steps to add the extension target and the necessary Pushwoosh code within it.

To ensure, that Notification Service Extension is properly integrated in your Flutter project, you need to use the following Podfile configuration:

Podfile
target 'NotificationServiceExtension' do
use_frameworks!
use_modular_headers!
pod 'PushwooshXCFramework'
inherit! :search_paths
end

To install dependencies for the iOS Flutter project, run the following command:

Terminal window
flutter run

or navigate to the ios folder in the terminal and run:

Terminal window
pod install --repo-update

Ensure that the required dependencies and plugins are added to your Gradle scripts:

Add the Google Services Gradle plugin to your project-level build.gradle dependencies:

android/build.gradle
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}

Apply the plugin in your app-level build.gradle file:

app/build.gradle
apply plugin: 'com.google.gms.google-services'

Place the google-services.json file into android/app folder in your project directory.

In your main/AndroidManifest.xml add Pushwoosh Device API Token inside the <application> tag:

<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />

Important: Be sure to give the token access to the right app in your Pushwoosh Control Panel. Learn more

  1. Build and run the project.
  2. Go to the Pushwoosh Control Panel and send a push notification.
  3. You should see the notification in the app.

At this stage, you have already integrated the SDK and can send and receive push notifications. Now, let’s explore the core functionality

In the Pushwoosh SDK there are two event listeners, designed for handling push notifications:

  • onPushReceived event is triggered, when a push notification is received
  • onPushAccepted event is triggered, when a user opens a notification

You should set up these event listeners right after initialization of the SDK at the application start up:

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class PushwooshNotificationHandler {
void setupPushListeners(Pushwoosh pushwoosh) {
pushwoosh.onPushReceived.listen((event) {
print("Push received: ${event.pushwooshMessage.payload}");
});
pushwoosh.onPushAccepted.listen((event) {
print("Push accepted: ${event.pushwooshMessage.payload}");
});
}
}

By focusing on individual user behavior and preferences, you can deliver personalized content, leading to increased user satisfaction and loyalty

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class Registration {
void afterUserLogin(User user) {
// Set user ID
Pushwoosh().setUserId(user.getId());
// Set user email
Pushwoosh().setEmail(user.getEmail());
// Register SMS number
// The SMS and WhatsApp numbers must be in E.164 format (e.g., "+1234567890") and be valid
Pushwoosh().registerSmsNumber(user.getSmsNumber());
// Register WhatsApp number
Pushwoosh().registerWhatsappNumber(user.getWhatsappNumber());
// Setting additional user information as tags for Pushwoosh
Pushwoosh().setTags({
"age": user.getAge(),
"name": user.getName(),
"last_login": user.getLastLoginDate()
});
}
}

Tags are key-value pairs assigned to users or devices, allowing segmentation based on attributes like preferences or behavior, enabling targeted messaging.

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class UpdateUser {
void afterUserUpdateProfile(User user) {
// Set list of favorite categories
Pushwoosh().setTags({
"favorite_categories": user.getFavoriteCategoriesList()
});
// Set payment information
Pushwoosh().setTags({
"is_subscribed": user.isSubscribed(),
"payment_status": user.getPaymentStatus(),
"billing_address": user.getBillingAddress()
});
}
}

Events are specific user actions or occurrences within the app that can be tracked to analyze behavior and trigger corresponding messages or actions

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class Registration {
// Track login event
void afterUserLogin(User user) {
Pushwoosh().postEvent("login", {
"name": user.getName(),
"last_login": user.getLastLoginDate()
});
}
void afterUserPurchase(Product product) {
// Track purchase event
Pushwoosh().postEvent("purchase", {
"product_id": product.getId(),
"product_name": product.getName(),
"price": product.getPrice(),
"quantity": product.getQuantity()
});
}
}

Thus, you may get this exception:

java.lang.IllegalStateException: Could not find class for name: com.pushwoosh.plugin.PushwooshNotificationServiceExtension

There are two solutions in this case:

  1. Use the flutter build apk --no-shrink command to compile your code without obfuscation.
  2. Or you can manually enable ProGuard and add the necessary rules.

To enable ProGuard for your project, add the following strings to your build.gradle file:

build.gradle
buildTypes {
release {
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}

Then, add the following rules to the android/app/proguard-rules.pro

proguard-rules.pro
#Pushwoosh Flutter
-keep class com.pushwoosh.plugin.PushwooshPlugin { *; }
-keep class com.pushwoosh.plugin.PushwooshNotificationServiceExtension { *; }

If you encounter any issues during the integration process, please refer to the support and community section.