Android Push Configuration

Adding meta for push notification

To show push notifications you need to add the notification small icon and large icon along with the AppId to the MoEngage.Builder

MoEngage.Builder builder = new MoEngage.Builder(this, "XXXXXXX")
        .SetNotificationSmallIcon(Resource.Drawable.icon)
        .SetNotificationLargeIcon(Resource.Drawable.ic_launcher)
      MoEInitializer.Initialize(builder.Build());

You can either let MoEngage SDK register for Push token and receive the push payload or in-case you already have a mechanism to do this you can pass the payload to the MoEngage SDK.

Before proceeding further make sure you have added the Firebase Dependency and added the google-services.json as explained in the documentation.

Push Registration and Receiving handled by App

By default, MoEngage SDK attempts to register for push token, since your application is handling push you need to opt-out of SDK's token registration.

How to opt-out of MoEngage Registration?

MoEngage.Builder builder = new MoEngage.Builder(this, "XXXXXXX")
        .SetNotificationSmallIcon(Resource.Drawable.icon)
        .SetNotificationLargeIcon(Resource.Drawable.ic_launcher)
        .OptOutTokenRegistration();
      MoEInitializer.Initialize(builder.Build());

If your app is registering for Push notification and receiving the payload the app would need to pass the push token and payload to the SDK.

How to pass push token?

Call the below method from the OnNewToken() of your Firebase Receiver

PushManager.Instance.RefreshToken(Application.ApplicationContext, token);

How to pass push payload?

On receiving the push payload inside OnMessageReceived you need to check whether the payload is from the MoEngage platform if the push is from MoEngage pass it to the SDK or else the app should handle the payload.

if (MoEngageNotificationUtils.IsFromMoEngagePlatform(payload.Data))
      {
        PushManager.Instance.PushHandler.HandlePushPayload(Application.ApplicationContext, payload.Data);
      }
      else
      {
        // app's logic for handling push notification  
      }

Push Registration and Receiving handled by SDK

Add the below code in your manifest file.

<service android:name="com.moengage.firebase.MoEFireBaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

When MoEngage SDK is handling Push registration it optionally provides a callback to the application whenever the push token is generated/refreshed.
To receive a callback implement PushManager.IOnTokenReceivedListener interface and register the callback in the Application class' OnCreate() using PushManager.Instance.SetTokenObserver()

Geofence Push

By default, SDK does not track location neither geo-fence campaigns work by default.
To track location and run geo-fence campaigns you need to opt-in for location service in the MoEngage initializer.
To initialize call the below opt-in API.

MoEngage.Builder builder = new MoEngage.Builder(this, "XXXXXX")
        .EnableLocationServices()
      MoEInitializer.Initialize(builder.Build());

🚧

Note

For Geo-fence pushes to work your Application should have location permission and Play Services' Location Library should be included in the App.

Showing Multiple Notifications at one go

By default, the SDK will update an exiting notification to keep the notification area uncluttered. But you can decide to switch to a model where the app shows multiple notifications. It can be achieved by
customizing the initializing object as shown below

MoEngage.Builder builder = new MoEngage.Builder(this, "XXXXXXX")
        .SetNotificationSmallIcon(Resource.Drawable.icon)
        .SetNotificationLargeIcon(Resource.Drawable.ic_launcher)
        .SetNotificationType(Resource.Integer.notification_type_multiple);
      MoEInitializer.Initialize(builder.Build());

Setting Notification Color

Lollipop and above only

MoEngage.Builder builder = new MoEngage.Builder(this, "XXXXXXX")
        .SetNotificationSmallIcon(Resource.Drawable.icon)
        .SetNotificationLargeIcon(Resource.Drawable.ic_launcher)
        .SetNotificationColor(Resource.Color.colorPrimary)
      MoEInitializer.Initialize(builder.Build());