Customize Service Notification

This guide will cover the customizations of the Android Service Notification which persists on the lock screen and in the notifications view while the SDK is actively transmitting locations.

Default notification

[info] Android docs

For more information on how Notifications work, refer to the Android documentation.

Basic customization

Use these basic UI customizations to edit the icon or text when the notification will not change dynamically.

Basic notification

Notification icon

By default, the notification's small icon is selected according to the icon specified in the android:icon tag in your AndroidManifest.xml file. You can customise the icons by adding drawable resources in your application with the same name as follows:

  • For the small icon, add an icon with the name ic_ht_service_notification_small
  • For the large icon, add an icon with the name ic_ht_service_notification_large

Notification text

By default, the notification text is "Touch here to open the app". You can customise the text by adding string resources in your application with the same name as ht_service_notification_text

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ht_service_notification_title">Driver App is running</string>
    <string name="ht_service_notification_text">Touch here to open the app</string>
</resources>

Notification click

By default, tapping the notification opens the LAUNCHER Activity specified in your AndroidManifest.xml file. To customise it call setServiceNotificationIntentClass on HTTransmitterService and pass the Class variable of the activity to it.

transmitterService =  HTTransmitterService.getInstance(this);
transmitterService.setServiceNotificationIntentClass(context, MainActivity.class);

Advanced customization

In case the notification needs to change dynamically or Android Rich Notification support needs to be added, follow these steps. These settings once set will persist till the App Data persists.

Advanced notification

Building notification params

To customize the Notification, we need to create a ServiceNotificationParams object. Following params can be customized using ServiceNotificationParamsBuilder object.

Parameters Description
smallIconResId required ResourceId for Small Icon for the Notification Layout and StatusBar Icon
smallIconBGColor Color Value for the Small Icon Background on the Notification layout
largeIconResId ResourceId for Large Icon for the Notification Layout
contentTitle Content Title for the Notification
contentText Content Text for the Notification
contentIntentActivityClassName Class Name for the Activity to be launched on Notification Click
setContentIntentExtras List of Extras to be passed along with Notification's intent
RemoteViews Inflated view for the custom layout to be set as Notification Layout
List List of Actions to be added in Notification Layout
// Get ServiceNotificationParamsBuilder Object
ServiceNotificationParamsBuilder notificationParamsBuilder = new ServiceNotificationParamsBuilder();

// List of Extras to be passed along with Notification Action's Intent
ArrayList<String> extras = new ArrayList<>();
extras.add(orderID);

// Create ServiceNotificationParams using ServiceNotificationParamsBuilder
final ServiceNotificationParams notificationParams = notificationParamsBuilder
    .setSmallIcon(R.drawable.ic_ht_service_notification_small)
    .setSmallIconBGColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent))
    .setContentTitle("Driver Demo is running")
    .setContextText("Touch here to open the Driver Demo app.")
    .setActionsList(actionList)
    .setContentIntentActivityClass(MainActivity.class)
    .setContentIntentExtras(extras)
    .build();

Set notification params

The Notification Params build can be set using the setServiceNotificationParams API in HTConsumerClient

TransmitterService transmitterService = HTTransmitterService.getInstance(this);
transmitterService.setServiceNotificationParams(notificationParams);

Clear notification params

Notification Params set using setServiceNotificationParams API are stored in persistent storage. In case the notification settings needs to be reset, call clearServiceNotificationParams API to reset to default settings.

TransmitterService transmitterService = HTTransmitterService.getInstance(this);
transmitterService.clearServiceNotificationParams();

Adding actions to the notification

The service notification provides support for Action that can be added as part of this notification. It must include an icon, a label, a Class name & an Intent type for the PendingIntent to be fired when the action is selected by the user.

  1. To add an Action to the notification, create an instance of ServiceNotificationAction as shown in the code snippet.
  2. Set a List of such ServiceNotificationAction objects to the notification using setActionsList method of ServiceNotificationParamsBuilder.
  3. Additionally, you can also provide a list of Strings as Extras which needs to be passed along with an Action's intent. Intent extras will contain this list as a key with the name TransmitterConstants.HT_SERVICE_NOTIFICATION_INTENT_EXTRAS_LIST.
// List of Extras to be passed along with Notification Action's Intent
ArrayList<String> extras = new ArrayList<>();
extras.add(orderID);

// Create an instance of ServiceNotificationAction
ServiceNotificationAction likeAction = new ServiceNotificationAction(R.drawable.ic_notification_like, "Like",
                     NotificationLikeButtonListener.class.getName(),
                      ServiceNotificationActionIntentType.ACTION_INTENT_TYPE_BROADCAST), extras);

[warning] Intent Activity's launchMode settings

In order for ServiceNotificationActionIntentType.ACTION_INTENT_TYPE_ACTIVITY Intents to work as expected, the Activity's launchMode has to be set to either singleTop or singleTask. Also, in case of already running Activities, the Intent has to be handled in Activity's onNewIntent method.

Identifying intent clicks

To identify the source of the intent click, the Intent of the click will contain an Extra by the name TransmitterConstants.HT_SERVICE_NOTIFICATION_INTENT_TYPE.

Click source Extra value
Notification TransmitterConstants.HT_SERVICE_NOTIFICATION_INTENT_TYPE_NOTIFICATION_CLICK
Action TransmitterConstants.HT_SERVICE_NOTIFICATION_INTENT_TYPE_ACTION_CLICK

With action clicks, the actionText param specified in the ServiceNotificationAction object is also sent as an Extra by the name TransmitterConstants.HT_SERVICE_NOTIFICATION_INTENT_ACTION_TEXT.

results matching ""

    No results matching ""