Push Configuration

Configuring Push in Android

To use Push Notification in your Flutter application you need to configure Firebase into your application, refer to the Push Notification documentation to configure Push Notification in your application.

In case, your application is handling the push token registration and push payload we highly recommend you use the native Android methods(mentioned in the documentation above) for passing the token and the payload to the SDK.
If for whatever reason you wish to pass the push token and payload to the SDK via the Flutter component/dart code use the below APIs

Passing Push Token

import 'package:moengage_flutter/moengage_flutter.dart';

final MoEngageFlutter _moengagePlugin = MoEngageFlutter();
_moengagePlugin.initialise();
_moengagePlugin.passFCMPushToken();

Passing Push Payload

import 'package:moengage_flutter/moengage_flutter.dart';

final MoEngageFlutter _moengagePlugin = MoEngageFlutter();
_moengagePlugin.initialise();
_moengagePlugin.passFCMPushPayload();

🚧

We highly recommend you to use the Android native APIs for passing the push payload to the MoEngage SDK instead of the Flutter/Dart APIs. Flutter Engine might not get initialized if the application is in the killed state which will lead to poor push reachability or delivery.

Customizing Push notification

If required the application can customize the behavior of notification by using Native Android code (Java/Kotlin). To learn more about the customisation refer to Advanced Push Configuration documentation.
Instead of extending PushMessageListener as mentioned in the above document extend PluginPushCallback

Refer to the below documentation for Push Amp+, Push Templates, Geofence.

Push Amp Plus

Push Templates

GeoFence Push

Configuring Push in iOS

APNS Certificate:

First, you will have to create an APNS certificate and upload it to the dashboard to be able to send push notifications in iOS. Follow the steps below to do that:

Follow the links on each step to complete it.

Project Capability Changes :

Once the APNS Certificate is uploaded, enable Push Entitlement in the Xcode project. For that select your app target, then go to Capabilities. Here enable the Push Notifications capability for your app. Also, we make use of silent pushes to track uninstalls. For tracking uninstalls of all the user, enable Remote Notification background mode in the app's capabilities as shown below:

Push Registration :

After this you will have to register for push notification by using registerForPushNotification method of the plugin as shown below:

import 'package:moengage_flutter/moengage_flutter.dart';

final MoEngageFlutter _moengagePlugin = MoEngageFlutter();
_moengagePlugin.initialise();
_moengagePlugin.registerForPushNotification();

Rich Push and Templates Support:

Please refer to the Native iOS SDK docs for supporting Rich Push(images/videos/audio in the notification) and Templates in the app:

Configuring Callbacks in Flutter

MoEngage's Flutter plugin optionally provides a callback on push click. To register for the callback call the setUpPushCallbacks() on the MoEngageFlutter object in your dart code.
This API takes a method as input with whose typedef is PushCallbackHandler(PushCampaign pushCampaign)

import 'package:moengage_flutter/moengage_flutter.dart';

  void _onPushClick(PushCampaign message) {
    print("This is a push click callback from native to flutter. Payload " +
        message.toString());
  }

final MoEngageFlutter _moengagePlugin = MoEngageFlutter();
_moengagePlugin.setUpPushCallbacks(_onPushClick);

Make sure this callback is set as soon as the application is initialized. Preferably in the initState() of your application widget.

Payload

NotificationPayload received in the callback is a PushCampaign instance with the following definition:

class PushCampaign {

  String platform;
  bool isDefaultAction;
  Map<String, dynamic> clickedAction;
  Map<String, dynamic> payload;
}

Payload Structure for clickedAction Map

{
  "clickedAction": {
    "type": "navigation/customAction",
    "payload": {
      "type": "screenName/deepLink/richLanding",
      "value": "",
      "kvPair": {
        "key1": "value1",
        "key2": "value2",
        ...
      }
    }
  }
}

platform - Native platform from which callback is triggered. Possible values - android, ios
isDefaultAction - This key is present only for the Android Platform. It's a boolean value indicating if the user clicked on the default content or not. true if the user clicks on the default content else false.
clickedAction - Action to be performed on notification click.
clickedAction.type - Type of click action. Possible values navigation and customAction. Currently, customAction is supported only on Android.
clickAction.payload - Action payload for the clicked action.
clickedAction.payload.type - Type of navigation action defined. Possible values screenName, deepLink, richLanding. Currently, in the case of iOS, richlanding and deep-link URL are processed internally by the SDK and not passed in this callback therefore possible value in case of iOS is only screenName.
clickAction.value - value entered for navigation action or custom payload.
clickAction.kvPair - Custom key-value pair entered on the MoEngage Platform.
payload - Complete campaign payload.

Android Payload

If the user clicks on the default content of the notification the key-value pair and campaign payload can be found inside the payload key. If the user clicks on the action button or a push template action the action payload would be found inside clickedAction.
You can use the isDefaultAction key to check whether the user clicked on the default content or not and then parse the payload accordingly.

iOS Payload

In the case of iOS, you would always receive the key-value pairs with respect to clicked action in clickedAction property. Refer to this link for knowing the iOS notification payload structure.