TrustedWebActivityService
abstract 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>
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 |
|
|---|---|
const String! |
ACTION_TRUSTED_WEB_ACTIVITY_SERVICE = "android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"An Intent Action used by the provider to find the TrustedWebActivityService or subclass. |
const String! |
KEY_SMALL_ICON_BITMAP = "android.support.customtabs.trusted.SMALL_ICON_BITMAP"The key to use to store a Bitmap to return from the |
const String! |
KEY_SUCCESS = "androidx.browser.trusted.SUCCESS"The key to use to store a boolean in the returns bundle of |
const String! |
META_DATA_NAME_SMALL_ICON = "android.support.customtabs.trusted.SMALL_ICON"The Android Manifest meta-data name to specify a small icon id to use. |
const Int |
SMALL_ICON_NOT_SET = -1Used as a return value of |
Public constructors |
|---|
Public functions |
|
|---|---|
abstract TokenStore |
Returns a |
Boolean |
@BinderThreadChecks whether notifications are enabled. |
IBinder? |
@MainThread |
Unit |
@BinderThreadCancels a notification. |
Unit |
Called by the system when the service is first created. |
Bundle? |
@BinderThreadContains a free form command from the browser. |
Bundle |
Returns a Bundle containing a bitmap to be use as the small icon for any notifications. |
Int |
Returns the Android resource id of a drawable to be used for the small icon of the notification. |
Boolean |
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)Displays a notification. |
Boolean |
@MainThread |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
ACTION_TRUSTED_WEB_ACTIVITY_SERVICE
const val ACTION_TRUSTED_WEB_ACTIVITY_SERVICE = "android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE": String!
An Intent Action used by the provider to find the TrustedWebActivityService or subclass.
KEY_SMALL_ICON_BITMAP
const val KEY_SMALL_ICON_BITMAP = "android.support.customtabs.trusted.SMALL_ICON_BITMAP": String!
The key to use to store a Bitmap to return from the onGetSmallIconBitmap method.
KEY_SUCCESS
const val KEY_SUCCESS = "androidx.browser.trusted.SUCCESS": String!
The key to use to store a boolean in the returns bundle of onExtraCommand method, to indicate whether the command is executed successfully.
META_DATA_NAME_SMALL_ICON
const val META_DATA_NAME_SMALL_ICON = "android.support.customtabs.trusted.SMALL_ICON": String!
The Android Manifest meta-data name to specify a small icon id to use.
SMALL_ICON_NOT_SET
const val SMALL_ICON_NOT_SET = -1: Int
Used as a return value of onGetSmallIconId when the icon is not provided.
Public constructors
Public functions
getTokenStore
@BinderThread
abstract fun getTokenStore(): TokenStore
Returns a TokenStore that is used to determine whether the connecting package is allowed to connect to this service.
| Returns | |
|---|---|
TokenStore |
An |
onAreNotificationsEnabled
@BinderThread
fun onAreNotificationsEnabled(channelName: String): Boolean
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. |
onCancelNotification
@BinderThread
fun onCancelNotification(platformTag: String, platformId: Int): Unit
Cancels a notification.
onCreate
@CallSuper
@MainThread
fun onCreate(): Unit
Called by the system when the service is first created. Do not call this method directly. Overrides must call super.onCreate().
onExtraCommand
@BinderThread
fun onExtraCommand(
commandName: String,
args: Bundle,
callbackRemote: TrustedWebActivityCallbackRemote?
): Bundle?
Contains a free form command from the browser. The client and browser will need to agree on an additional API to use in advanced. This call can be used for testing or experimental purposes. A return value of null will be used to signify that the client does not know how to handle the request. As optional best practices, KEY_SUCCESS could be use to identify that command was *successfully* handled. For example, when returning a message with result:
Bundle result = new Bundle(); result.putString("message", message); if (success) result.putBoolean(KEY_SUCCESS, true); return result;
Bundle result = service.extraCommand(commandName, args); if (result.getBoolean(service.KEY_SUCCESS)) { // Command was successfully handled }
| Parameters | |
|---|---|
commandName: String |
Name of the command to execute. |
args: Bundle |
Arguments to the command. |
callbackRemote: TrustedWebActivityCallbackRemote? |
Contains the callback that passed with the command. |
onGetSmallIconBitmap
@BinderThread
fun onGetSmallIconBitmap(): Bundle
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 |
onGetSmallIconId
@BinderThread
fun onGetSmallIconId(): 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.
| Returns | |
|---|---|
Int |
A resource id for the small icon, or |
onNotifyNotificationWithChannel
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)
@BinderThread
fun onNotifyNotificationWithChannel(
platformTag: String,
platformId: Int,
notification: Notification,
channelName: String
): Boolean
Displays a notification.
| Parameters | |
|---|---|
platformTag: String |
The notification tag, see |
platformId: Int |
The notification id, see |
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). |