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

[warning] Why a persistent notification and is it possible to remove it?

For more information on why a persistant notification is required for HyperTrack SDK and for a way to remove it, refer to the FAQs section.

Basic customization

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

[info] Android docs

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

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 HyperTrack.setServiceNotificationParams() API with an instance of ServiceNotificationParams as depicted in the next section.

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 Notification.Action to be added in Notification Layout
// Get ServiceNotificationParamsBuilder Object
ServiceNotificationParamsBuilder builder = 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 = builder
    .setSmallIcon(R.drawable.ic_ht_service_notification_small)
    .setSmallIconBGColor(ContextCompat.getColor(getApplication(), 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 HyperTrack.setServiceNotificationParams() API

HyperTrack.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 HyperTrack.clearServiceNotificationParams API to reset to default settings.

HyperTrack.clearServiceNotificationParams();

Adding Notification actions to the notification

The service notification provides support for Notification.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 Notification.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 Notification.Action's intent. Intent extras will contain this list as a key with the name HyperTrackConstants.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 HyperTrackConstants.HT_SERVICE_NOTIFICATION_INTENT_TYPE.

Click source Extra value
Notification HyperTrackConstants.HT_SERVICE_NOTIFICATION_INTENT_TYPE_NOTIFICATION_CLICK
Action HyperTrackConstants.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 HyperTrackConstants.HT_SERVICE_NOTIFICATION_INTENT_ACTION_TEXT.

results matching ""

    No results matching ""