Skip to main content

Handling interaction with notifications that contain URLs

Ometria allows you to send URLs and tracking info alongside your push notifications and allows you to handle them on the device.

By default, the Ometria SDK automatically handles notification interactions containing URLs by launching an ACTION_VIEW intent for the URL. The link is opened by any application on the device capable of handling it (e.g., a browser or an app registered for the corresponding deep link).

However, it enables developers to handle those URLs as they see fit (e.g. take the user to a specific screen in the app).

To get access to those interactions and the URLs, implement the OmetriaNotificationInteractionHandler

There is only one method that is required, and it will be triggered every time the user taps on a notification that has a deepLink action URL.

This is what it would look like in code:

class SampleApp : Application(), OmetriaNotificationInteractionHandler {
companion object {
lateinit var instance: SampleApp
}

override fun onCreate() {
super.onCreate()
instance = this

// Initializing Ometria SDK with application context, api token and notifications icon resource id
// Note: Replace api token with your own
Ometria.initialize(
this,
"YOUR_API_KEY",
R.mipmap.ic_launcher
).loggingEnabled(true)

// Set the notificationInteractionDelegate in order to provide actions for
// notifications that contain a deepLink URL.
// The default functionality when you don't assign a delegate is opening urls in a browser
Ometria.instance().notificationInteractionHandler = this
}

/**
* This method will be called each time the user interacts with a notification from Ometria.
* Write your own custom code in order to properly redirect the app to the screen that should be displayed.
*/
override fun onNotificationInteraction(ometriaNotification: OmetriaNotification) {
Log.d(SampleApp::class.java.simpleName, "URL: ${ometriaNotification.deepLinkActionUrl}")
}
}

The OmetriaNotification object also provides access to other fields in the notification payload, including custom tracking properties that you choose to send.

Task preservation and Legacy Mode​

Starting with Android SDK 1.11.5, notification taps can preserve the existing application task instead of recreating it.

Legacy Mode is controlled by setClearTaskOnNotificationTap(clearTaskOnNotificationTap: Boolean). It is enabled by default for backwards compatibility.

Ometria.initialize(
application = this,
apiToken = "YOUR_API_KEY",
notificationIcon = R.drawable.ic_notification_nys
).setClearTaskOnNotificationTap(false)
Legacy ModeBehaviour
OFF, setClearTaskOnNotificationTap(false)The existing task is reused whenever possible. The back stack is preserved and the host activity receives onNewIntent() for warm starts.
ON, setClearTaskOnNotificationTap(true)Previous SDK behaviour is retained. The task is recreated and onCreate() is invoked.

This setting only controls task recreation behaviour. The internal notification trampoline activity is always handled by the SDK and is not configurable.

Expected lifecycle behaviour​

Application stateLegacy Mode OFFLegacy Mode ON
App in backgroundonNewIntent()onCreate()
Process killedonCreate()onCreate()
App removed from RecentsonCreate()onCreate()

Existing integrations do not need to change anything if Legacy Mode remains enabled. If you disable Legacy Mode to preserve the existing task, make sure your activity handles onNewIntent() correctly.

Notifications posted before upgrading the SDK continue using the previous behaviour because their PendingIntent already contains the original launch flags.

If for some reason developers need access to the OmetriaNotification object in a context other than the OmetriaNotificationInteraction, Ometria SDK provides a method called fun parseNotification(remoteMessage: RemoteMessage) for this:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
val ometriaNotification = Ometria.instance().parseNotification(remoteMessage)
}