Configuring push notifications (Flutter)

Prerequisites


Firebase Cloud Messaging

Google Firebase Cloud Messaging is necessary to handle push notifications sent from Synerise.

  1. Follow the instructions in this article and integrate the Firebase plugin with your application.
  2. Follow the instructions in this article and integrate cloud messaging in your application.
  3. Integrate Firebase with Synerise. See Integration section.
Note: It’s important that the Firebase plugin is initialized as early as possible in the application lifecycle and also after the Synerise SDK. Late initialization may cause compilation problems.

Setting up - Android


Requirements

  1. After configuring Firebase, add the google-services.json file to your project.

  2. Add the google-services dependency to your project’s build.gradle file.

      dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.3'
        ...
      }
      

Setting up - iOS


Requirements

  1. Configure handling Push Notifications in your application. See Apple Notifications.

  2. After configuring Firebase, add the GoogleService-Info.plist file to your project.

  3. Make sure your Info.plist file contains the following snippet:

<false/><key>FirebaseAppDelegateProxyEnabled</key><false/>

Extensions for push notifications

Notification Service Extension

Synerise Notification Service Extension is an object that adds the notification functionality to the SDK.

It implements the following operations:

  • Decrypting Simple Push communication data (if encryption is enabled).
  • Tracking events from Simple Push communication.
  • Adding action buttons to Simple Push communication (if the communication contains any).
  • Improving the appearance of Simple Push communication (Rich Media - Single Image) with an image thumbnail.

Notification Service Extension should be implemented in the native part of the application. Follow the instructions in this article.

Rich Media Notification Content Extensions

Synerise Rich Media Notification Content Extension is an object that allows rendering your own appearance of a push notification when the notification is expanded (by tapping the notification).

Synerise Rich Media Notification Content Extensions should be implemented in the native part of the application. Follow the instructions in this article.

Set up Firebase Cloud Messaging for Synerise SDK


The following code example explains how to implement Firebase Cloud Messaging integration with Synerise:

Request for permission from the user.

await FirebaseMessaging.instance.requestPermission(
  alert: true,
  announcement: false,
  badge: true,
  carPlay: false,
  criticalAlert: false,
  provisional: false,
  sound: true,
);

Set presentation options for foreground state.

FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
  alert: true,
  sound: true,
  badge: true
);

Set background message handler.

FirebaseMessaging.onBackgroundMessage(backgroundHandlerForFCM);

@pragma('vm:entry-point')
Future<void> backgroundHandlerForFCM(RemoteMessage message) async {
  await Firebase.initializeApp();
  await Synerise.initializer()
    .withClientApiKey("YOUR_PROFILE_API_KEY")
    .withBaseUrl("YOUR_API_BASE_URL")
    .withDebugModeEnabled(false)
    .init();

  await Synerise.notifications.handleNotification(message.toMap());

Get Firebase FCM token and implement listeners so our SDK can receive the Firebase token that is required to deliver push notifications from Synerise.

FirebaseMessaging.instance.getToken().then((value) {
  if (value != null) {
    Synerise.notifications.registerForNotifications(value, true);
}
});
FirebaseMessaging.instance.onTokenRefresh.listen((event) {
  FirebaseMessaging.instance.getToken().then((value) {
    if (value != null) {
      Synerise.notifications.registerForNotifications(value, true);
    }
  });
});

Make sure that the Firebase FCM token is always up-to-date by implementing the onRegistrationRequired() method.

Synerise.notifications.listener((listener) {
  listener.onRegistrationRequired = () {
    Synerise.notifications.registerForNotifications(firebaseToken!, true);
  };
});

Important: The second parameter of the registration method is the agreement for mobile push campaigns. In the Profile’s card in Synerise, you can find it in the Subscriptions section (if you have the required access permission). Learn more about the Synerise.notifications.registerForNotifications(registrationToken, mobileAgreement) method in the method reference.

Configure Notification Encryption


Android

See Configure Notification Encryption.

iOS

See Synerise Notification Service Extension and Configure Notification Encryption.

Application implementation

In the application, you must set encryption to true in the SDK initializer or in the SDK settings.

// WARNING: This option must be configured before Synerise SDK is initialized!
Synerise.settings.notifications.encryption = true;

Handling incoming push notifications


Note: You may disable handling push notifications in the SDK at any time. See Enable/disable notifications.

Synerise payload

The following code introduces how to handle push notifications:

FirebaseMessaging.onMessage.listen((RemoteMessage message,) {
  Synerise.notifications.handleNotification(message.toMap());
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  Synerise.notifications.handleNotificationClick(message.toMap());
});

Handling actions from push notifications


Additional in-app alert from push notifications


The Flutter SDK on iOS devices can display an additional alert in the application after a push notification is received. See this article to read more about this feature.

Simple Push campaign with in-app alert
Simple Push campaign with in-app alert

Limitations compared to native platforms


Due to platform limitations, not all notification functionalities may work as in native SDKs.

  • iOS only: Native-configured button from a Simple Push campaign always invokes the default action (if configured) or displays an in-app alert with buttons to choose.
😕

We are sorry to hear that

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

😉

Awesome!

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

Close modal icon Placeholder alt for modal to satisfy link checker