TrustedWebActivityService

open class TrustedWebActivityService : Service
kotlin.Any
   ↳ 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:

<service android:name="androidx.browser.trusted.TrustedWebActivityService" android:enabled="true" android:exported="true">
 
      
 <meta-data android:name="android.support.customtabs.trusted.SMALL_ICON" android:resource="@drawable/ic_notification_icon" />
 
      
 <intent-filter> 
  <action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
 </intent-filter>
  
</service>
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 to getSmallIconId, notifyNotificationWithChannel and cancelNotification can occur on different Binder threads, so overriding implementations need to be thread-safe.

For security, the TrustedWebActivityService will check that whatever connects to it is the Trusted Web Activity provider that it was previously verified with (through setVerifiedProvider).

Summary

Constants

static String

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

static String

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

static String

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

static Int

Used as a return value of getSmallIconId when the icon is not provided.

Public constructors

The TrustedWebActivityService lives in a client app and serves requests from a Trusted Web Activity provider.

Public methods

open Boolean
areNotificationsEnabled(@NonNull channelName: String)

Checks whether notifications are enabled.

open Unit
cancelNotification(@NonNull platformTag: String, platformId: Int)

Cancels a notification.

open Bundle

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

open Int

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

open Boolean
notifyNotificationWithChannel(@NonNull platformTag: String, platformId: Int, @NonNull notification: Notification, @NonNull channelName: String)

Displays a notification.

IBinder?
onBind(@Nullable intent: Intent?)

open Unit

Called by the system when the service is first created.

Boolean
onUnbind(@Nullable intent: Intent?)

static Unit
setVerifiedProvider(@NonNull context: Context, @Nullable provider: String?)

Sets (asynchronously) the package that this service will accept connections from.

Constants

ACTION_TRUSTED_WEB_ACTIVITY_SERVICE

static val ACTION_TRUSTED_WEB_ACTIVITY_SERVICE: String

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

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

KEY_SMALL_ICON_BITMAP

static val KEY_SMALL_ICON_BITMAP: String

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

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

META_DATA_NAME_SMALL_ICON

static val META_DATA_NAME_SMALL_ICON: String

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

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

SMALL_ICON_NOT_SET

static val SMALL_ICON_NOT_SET: Int

Used as a return value of getSmallIconId when the icon is not provided.

Value: -1

Public constructors

<init>

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:

<service android:name="androidx.browser.trusted.TrustedWebActivityService" android:enabled="true" android:exported="true">
 
      
 <meta-data android:name="android.support.customtabs.trusted.SMALL_ICON" android:resource="@drawable/ic_notification_icon" />
 
      
 <intent-filter> 
  <action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE" /> 
  <category android:name="android.intent.category.DEFAULT" /> 
 </intent-filter>
  
</service>
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 to getSmallIconId, notifyNotificationWithChannel and cancelNotification can occur on different Binder threads, so overriding implementations need to be thread-safe.

For security, the TrustedWebActivityService will check that whatever connects to it is the Trusted Web Activity provider that it was previously verified with (through setVerifiedProvider).

Public methods

areNotificationsEnabled

open fun areNotificationsEnabled(@NonNull channelName: String): Boolean

Checks whether notifications are enabled.

Parameters
channelName String: The name of the notification channel to be used on Android O+.
Return
Boolean: Whether notifications are enabled.

cancelNotification

open fun cancelNotification(@NonNull platformTag: String, platformId: Int): Unit

Cancels a notification.

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

getSmallIconBitmap

@NonNull open fun getSmallIconBitmap(): Bundle

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

Return
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.

getSmallIconId

open fun getSmallIconId(): Int

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.

Return
Int: A resource id for the small icon, or SMALL_ICON_NOT_SET if not found.

notifyNotificationWithChannel

open fun notifyNotificationWithChannel(@NonNull platformTag: String, platformId: Int, @NonNull notification: Notification, @NonNull channelName: String): Boolean

Displays a notification.

Parameters
platformTag String: The notification tag, see NotificationManager#notify(String, int, Notification).
platformId String: The notification id, see NotificationManager#notify(String, int, Notification).
notification String: 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.
Return
Boolean: Whether the notification was successfully displayed (the channel/app may be blocked by the user).

onBind

@Nullable fun onBind(@Nullable intent: Intent?): IBinder?

onCreate

@CallSuper open fun onCreate(): Unit

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

onUnbind

fun onUnbind(@Nullable intent: Intent?): Boolean

setVerifiedProvider

static fun setVerifiedProvider(@NonNull context: Context, @Nullable provider: String?): Unit

Sets (asynchronously) the package that this service will accept connections from.

Parameters
context Context: A context to be used to access SharedPreferences.
provider Context: The package of the provider to accept connections from or null to clear.