Android push notifications
Enable push notifications on Android using the MAUI SDK
The MAUI SDK relies on the native Android SDK to handle push notifications on Android. This guide provides shortened instructions for Android within the context of the MAUI SDK and refers to the push notifications documentation for the Android SDK for details.
The behaviour of push notification delivery and click tracking may be affected by the tracking consent feature, which, if enabled, requires explicit consent for tracking. Refer to the tracking consent documentation for details.
Integration
Exponea Android SDK supports the following integrations:
Standard (Firebase) integration
To be able to send push notifications from the Engagement platform and receive them in your app on Android devices, you must:
- Set up a Firebase project.
- Implement Firebase messaging in your app.
- Configure the Firebase Cloud Messaging integration in the Engagement web app.
Please note that with Google deprecating and removing the FCM legacy API in June 2024, Bloomreach Engagement is now using Firebase HTTP v1 API.
Set up Firebase
Follow the instructions in Set up Firebase for the Android SDK.
Implement Firebase messaging in your app
To handle incoming push messages, you must create and register a service that extends FirebaseMessagingService
, which calls the SDK method HandleRemoteMessage
when the message is received and HandlePushToken
when the token is obtained from the Firebase:
...
using Android.App;
using Bloomreach;
using Firebase.Messaging;
namespace YourNameSpace
{
[Service(Name = "yournamespace.ExampleFirebaseMessageService", Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class ExampleFirebaseMessageService : FirebaseMessagingService {
public override void OnMessageReceived(RemoteMessage message)
{
if (!Bloomreach.BloomreachSDK.HandleRemoteMessage(NotificationPayload.Parse(message.Data)))
{
Console.WriteLine("Remote Message received without Bloomreach content");
}
}
public override void OnNewToken(string token)
{
Bloomreach.BloomreachSDK.HandlePushToken(token);
}
}
}
Configure the Firebase Cloud Messaging integration in Engagement
Follow the instructions in Configure the Firebase Cloud Messaging integration in Engagement for the Android SDK.
Huawei integration
To be able to send push notifications from the Engagement platform and receive them in your app on Huawei devices, you must:
- Set up Huawei Mobile Services (HMS)
- Implement HMS in your app.
- Configure the Huawei Push Service integration in the Engagement web app.
Set up Huawei Mobile Services
First, you must set up Huawei Mobile Services:
- Register and set up a Huawei Developer account.
- Create a Project and App in AppGallery Connect.
- Generate and configure a Signing Certificate.
- Enable Push Kit in AppGallery Connect APIs.
- Update Gradle scripts and add generated
agconnect-services.json
to your app. - Configure the Signing Information in your app.
For detailed instructions, please refer to Preparations for Integrating HUAWEI HMS Core in the official HMS documentation.
We also recommend adding this line to AssemblyInfo.cs
in your app:
[assembly: MetaData("push_kit_auto_init_enabled", Value = "true")]
Implement HMS Message Service in your app
To handle incoming push messages, you must create and register a service that extends HmsMessageService, which calls the SDK method HandleRemoteMessage
when the message is received and HandleHmsPushToken
when the token is obtained from the Huawei Messaging Service.
...
using Android.App;
using Bloomreach;
using Huawei.Hms.Push;
namespace YourNameSpace
{
[Service(Name = "yournamespace.ExampleHuaweiMessageService", Exported = false)]
[IntentFilter(new[] { "com.huawei.push.action.MESSAGING_EVENT" })]
public class ExampleHuaweiMessageService : HmsMessageService {
public override void OnMessageReceived(RemoteMessage message)
{
if (!Bloomreach.BloomreachSDK.HandleRemoteMessage(NotificationPayload.Parse(message.DataOfMap)))
{
Console.WriteLine("Remote Message received without Bloomreach content");
}
}
public override void OnNewToken(string token)
{
Bloomreach.BloomreachSDK.HandleHmsPushToken(token);
}
}
}
The SDK will only handle push notification messages sent from the Engagement platform. A helper method IsBloomreachNotification()
is also provided.
Configure the Huawei Push Service integration in Engagement
Follow the instructions in Configure the Huawei Push Service integration in Engagement for the Android SDK.
Customization
Configure automatic push notification tracking
By default, the SDK tracks push notifications automatically. In the SDK configuration, you can set the desired frequency using the TokenTrackFrequency
property (default value is ON_TOKEN_CHANGE
). You can also disable automatic push notification tracking by setting the Boolean value of the AutomaticPushNotification
property to false
.
If AutomaticPushNotification
is enabled, the SDK will display push notifications from Engagement and track a "campaign" event for every delivered/opened push notification with the relevant properties.
The behaviour of push notification delivery and click tracking may be affected by the tracking consent feature, which, if enabled, requires explicit consent for tracking. Refer to the tracking consent documentation for details.
Respond to push notifications
When creating a push notification in the Engagement web app, you can choose from three different actions to be performed when tapping the notification or additional buttons displayed with the notification.
Open app
The "Open app" action generates an intent with action com.exponea.sdk.action.PUSH_CLICKED
. To respond to it, you must set up a BroadcastReceiver
:
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
namespace MauiExample.Droid
{
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { "com.exponea.sdk.action.PUSH_CLICKED" })]
public class MyReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Extract payload data
NotificationData value = (NotificationData)intent.GetParcelableExtra("NotificationData");
// Process the data if you need to
foreach (KeyValuePair<string, Java.Lang.Object> entry in value.Attributes)
{
Console.WriteLine(entry.Key + ":" + entry.Value);
}
// Do not start an Activity here. It is forbidden when targetting Android 12. The SDK will start the activity on push open for you
}
}
}
In the BroadcastReceiver
, you can launch a corresponding activity (for example, your main activity). Campaign data is included in the intent as NotificationData
.
Deep link
The "Deep link" action creates a "view" intent that contains the URL specified when setting up the action in Engagement. To respond to this intent, create an intent filter on the activity that handles it:
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "your_scheme",
DataHost = "your_host",
DataPathPattern = "your_path_pattern",
AutoVerify = true
)]
public class MainActivity : MauiAppCompatActivity
{
...
Open web browser
The "Open web browser" action is handled automatically by the SDK and no work is required from the developer to handle it.
Handle additional data payload
When creating a push notification in the Engagement web app, you can set it up to contain additional data. Whenever a notification arrives, the SDK will call ReceivedPushCallback
, which you can set on the BloomreachSDK
instance. The additional data is provided as a IDictionary<string,object>
.
Action<IDictionary<string, object>> action = (extra) =>
{
//handle extra values
};
Bloomreach.BloomreachSDK.SetReceivedPushCallback(action);
Note that if the SDK previously received any additional data while no listener was attached to the callback, it will dispatch that data as soon as a listener is attached.
Silent push notifications
The Engagement web app allows you to set up silent push notifications, that are not displayed to the user. The SDK tracks a campaign
event when a silent push notification is delivered. Silent push notifications cannot be opened but if you have set up extra data in the payload, the SDK will call ReceivedPushCallback
as described in Handle additional data payload.
Manually track push notifications
If you disable automatic push notification tracking or if you want to track push notifications from other providers, you can manually track events related to push notifications.
Track push token (FCM)
Use the TrackPushToken
method to manually track the FCM push token:
Bloomreach.BloomreachSDK.TrackPushToken("382d4221-3441-44b7-a676-3eb5f515157f");
Invoking this method will track the push token immediately regardless of the SDK configuration for TokenTrackFrequency
.
Track delivered push notification
Use the TrackDeliveredPush
method to manually track a delivered push notification:
Bloomreach.BloomreachSDK.TrackDeliveredPush(NotificationPayload.Parse(data));
Track clicked push notification
Use the TrackClickedPush
method to manually track a clicked push notification:
Bloomreach.BloomreachSDK.TrackClickedPush(NotificationAction("action", "name", "url").WithAttributes(data));
The behaviour of push notification delivery and click tracking may be affected by the tracking consent feature, which, if enabled, requires explicit consent for tracking. Refer to the tracking consent documentation for details.
Updated about 1 month ago