Android Push Configuration
warning

Critical

Microsoft is replacing Xamarin with .NET MAUI and the support for the latest release of Xamarin is till November 2023. MoEngage will stop providing updates for the MoEngage Xamarin SDK due to the deprecation. Deprecation impacts Xamarin In-App Messaging and will stop working after June 2022. All other features will continue to work in the Xamarin SDK until November 2023.

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

C#
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?

C#
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

C#
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.

C#
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 to your manifest file.

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()

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

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.

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

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

C#
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

C#
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());
Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?