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.passPushToken();

Passing Push Payload

import 'package:moengage_flutter/moengage_flutter.dart';

final MoEngageFlutter _moengagePlugin = MoEngageFlutter();
_moengagePlugin.passPushPayload();

To use the above APIs make sure you have updated the MoEngage package 1.1.0 or higher and the Android SDK dependency is 9.8.03 or higher.

🚧

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.

Configuring Push in iOS

APNS Certificate :

First you will have to create an APNS certificate and upload in 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 👎

2372

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.registerForPushNotification();

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 MessageHandler(Map<String, dynamic> message)

import 'package:moengage_flutter/moengage_flutter.dart';

  void _onPushClick(Map<String, dynamic> message) {
    print("This is a push click callback from native to flutter. Payload " +
        message.toString());
  }

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

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

Push Payload

Push Payload will be received in the below format:

{
  "platform": "ios/android",
  "payload": {
    // platform specific payload
  }
}

Android Payload

All the key-value pair entered on the MoEngage Dashboard during the campaign will be available inside the payload object.

iOS Payload

Refer to this link for iOS notification payload structure.