onMessageOpened

protected open fun onMessageOpened(message: PushMessage)

Callback invoked when a user taps on a notification.

Override this method to handle notification open events, such as tracking analytics, updating application state, or triggering custom logic.

This method is called on the main thread. For a tap on the notification body or on an open, url or deeplink action button it runs after startActivityForPushMessage. For a custom action button it runs inside a broadcast receiver with no activity started and roughly ten seconds of budget: move long work off this thread yourself, and do not start an activity from here — Android 12+ forbids it. Check getTappedButtonId and the matching getAction before navigating. Example:



protected void onMessageOpened(PushMessage message) {
    // Track notification open in analytics
    String customData = message.getCustomData();
    if (customData != null) {
        try {
            String campaignCode = new JSONObject(customData).optString("campaign_code", null);
            Analytics.track("push_opened", campaignCode);
        } catch (JSONException e) {
            Log.e("App", "Failed to parse custom data", e);
        }
    }

    // Update user engagement score
    SharedPreferences prefs = getApplicationContext()
        .getSharedPreferences("user_stats", Context.MODE_PRIVATE);
    int openCount = prefs.getInt("push_open_count", 0);
    prefs.edit().putInt("push_open_count", openCount + 1).apply();

    // Log for debugging
    Log.d("PushNotifications", "User opened: " + message.getMessage());
}

Parameters

message

the push message that was opened, containing notification data and custom payload

See also