The Android Developer Challenge is back! Submit your idea before December 2.

TrustedWebActivityService

public abstract class TrustedWebActivityService
extends Service

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ androidx.browser.trusted.TrustedWebActivityService


The TrustedWebActivityService lives in a client app and serves requests from a Trusted Web Activity provider. At present it only serves requests to do with notifications.

When the provider receives a notification from a scope that is associated with a Trusted Web Activity client app, it will attempt to connect to a TrustedWebActivityService and forward calls. This allows the client app to display the notifications itself, meaning it is attributable to the client app and is managed by notification permissions of the client app, not the provider.

TrustedWebActivityService is usable as it is, by adding the following to your AndroidManifest:

 

     

     
         
         
     
 
 
The SMALL_ICON resource should point to a drawable to be used for the notification's small icon.

Alternatively for greater customization, TrustedWebActivityService can be extended and overridden. In this case the manifest entry should be updated to point to the extending class.

As this is an AIDL Service, calls may come in from different Binder threads, so overriding implementations need to be thread safe [1].

For security, the TrustedWebActivityService will check that whatever connects to it matches the Token stored in the TokenStore returned by getTokenStore(). This is because we don't want to allow any app on the users device to connect to this Service be able to make it display notifications. [1]: https://developer.android.com/guide/components/aidl.html

Summary

Constants

String ACTION_TRUSTED_WEB_ACTIVITY_SERVICE

An Intent Action used by the provider to find the TrustedWebActivityService or subclass.

String KEY_SMALL_ICON_BITMAP

The key to use to store a Bitmap to return from the onGetSmallIconBitmap() method.

String META_DATA_NAME_SMALL_ICON

The Android Manifest meta-data name to specify a small icon id to use.

int SMALL_ICON_NOT_SET

Used as a return value of onGetSmallIconId() when the icon is not provided.

Inherited constants

Public constructors

TrustedWebActivityService()

Public methods

abstract TokenStore getTokenStore()

Returns a TokenStore that is used to determine whether the connecting package is allowed to connect to this service.

boolean onAreNotificationsEnabled(String channelName)

Checks whether notifications are enabled.

final IBinder onBind(Intent intent)
void onCancelNotification(String platformTag, int platformId)

Cancels a notification.

void onCreate()

Called by the system when the service is first created.

Bundle onGetSmallIconBitmap()

Returns a Bundle containing a bitmap to be use as the small icon for any notifications.

int onGetSmallIconId()

Returns the Android resource id of a drawable to be used for the small icon of the notification.

boolean onNotifyNotificationWithChannel(String platformTag, int platformId, Notification notification, String channelName)

Displays a notification.

final boolean onUnbind(Intent intent)

Inherited methods

Constants

ACTION_TRUSTED_WEB_ACTIVITY_SERVICE

public static final String ACTION_TRUSTED_WEB_ACTIVITY_SERVICE

An Intent Action used by the provider to find the TrustedWebActivityService or subclass.

Constant Value: "android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"

KEY_SMALL_ICON_BITMAP

public static final String KEY_SMALL_ICON_BITMAP

The key to use to store a Bitmap to return from the onGetSmallIconBitmap() method.

Constant Value: "android.support.customtabs.trusted.SMALL_ICON_BITMAP"

META_DATA_NAME_SMALL_ICON

public static final String META_DATA_NAME_SMALL_ICON

The Android Manifest meta-data name to specify a small icon id to use.

Constant Value: "android.support.customtabs.trusted.SMALL_ICON"

SMALL_ICON_NOT_SET

public static final int SMALL_ICON_NOT_SET

Used as a return value of onGetSmallIconId() when the icon is not provided.

Constant Value: -1 (0xffffffff)

Public constructors

TrustedWebActivityService

public TrustedWebActivityService ()

Public methods

getTokenStore

public abstract TokenStore getTokenStore ()

Returns a TokenStore that is used to determine whether the connecting package is allowed to connect to this service.

Returns
TokenStore An TokenStore containing the verified provider.

onAreNotificationsEnabled

public boolean onAreNotificationsEnabled (String channelName)

Checks whether notifications are enabled.

Parameters
channelName String: The name of the notification channel to be used on Android O+.

Returns
boolean Whether notifications are enabled.

onBind

public final IBinder onBind (Intent intent)

Parameters
intent Intent

Returns
IBinder

onCancelNotification

public void onCancelNotification (String platformTag, 
                int platformId)

Cancels a notification.

Parameters
platformTag String: The notification tag, see NotificationManager.cancel(String, int).

platformId int: The notification id, see NotificationManager.cancel(String, int).

onCreate

public void onCreate ()

Called by the system when the service is first created. Do not call this method directly. Overrides must call super.onCreate().

onGetSmallIconBitmap

public Bundle onGetSmallIconBitmap ()

Returns a Bundle containing a bitmap to be use as the small icon for any notifications.

Returns
Bundle A Bundle that may contain a Bitmap contained with key KEY_SMALL_ICON_BITMAP. The bundle may be empty if the client app does not provide a small icon.

onGetSmallIconId

public int onGetSmallIconId ()

Returns the Android resource id of a drawable to be used for the small icon of the notification. This is called by the provider as it is constructing the notification so a complete notification can be passed to the client. Default behaviour looks for meta-data with the name META_DATA_NAME_SMALL_ICON in service section of the manifest.

Returns
int A resource id for the small icon, or SMALL_ICON_NOT_SET if not found.

onNotifyNotificationWithChannel

public boolean onNotifyNotificationWithChannel (String platformTag, 
                int platformId, 
                Notification notification, 
                String channelName)

Displays a notification.

Parameters
platformTag String: The notification tag, see NotificationManager.notify(String, int, Notification).

platformId int: The notification id, see NotificationManager.notify(String, int, Notification).

notification Notification: The notification to be displayed, constructed by the provider.

channelName String: The name of the notification channel that the notification should be displayed on. This method gets or creates a channel from the name and modifies the notification to use that channel.

Returns
boolean Whether the notification was successfully displayed (the channel/app may be blocked by the user).

onUnbind

public final boolean onUnbind (Intent intent)

Parameters
intent Intent

Returns
boolean