startActivityForPushMessage

protected open fun startActivityForPushMessage(message: PushMessage)

Callback invoked to launch an activity when a notification is opened.

Override this method to customize which activity opens when the user taps on a notification. This is the primary method for implementing custom deep linking, routing users to specific screens based on notification data, or adding custom launch logic.

Default Behavior: By default, this method attempts to start an activity in the following order:

  1. Activity with intent filter action: {applicationId}.MESSAGE
  2. Default launcher activity (if no MESSAGE activity found)

Important: This method runs on the main thread. It's safe to update UI but avoid long-running operations. Example 1: Route to different activities based on notification data



protected void startActivityForPushMessage(PushMessage message) {
    Context context = getApplicationContext();
    String customData = message.getCustomData();
    if (customData == null) {
        super.startActivityForPushMessage(message);
        return;
    }

    try {
        JSONObject data = new JSONObject(customData);
        String screenType = data.optString("screen");

        Intent intent;
        if ("product".equals(screenType)) {
            // Open product details
            intent = new Intent(context, ProductActivity.class);
            String productId = data.optString("product_id", null);
            intent.putExtra("productId", productId);

        } else if ("order".equals(screenType)) {
            // Open order details
            intent = new Intent(context, OrderActivity.class);
            String orderId = data.optString("order_id", null);
            intent.putExtra("orderId", orderId);

        } else if ("cart".equals(screenType)) {
            // Open shopping cart
            intent = new Intent(context, CartActivity.class);

        } else {
            // Default behavior for unknown screen types
            super.startActivityForPushMessage(message);
            return;
        }

        // Required flags for starting activity from non-activity context
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_SINGLE_TOP);

        // Pass full push message data
        intent.putExtra("pushMessage", message.toJson().toString());

        context.startActivity(intent);
    } catch (JSONException e) {
        Log.e("App", "Failed to parse custom data", e);
        super.startActivityForPushMessage(message);
    }
}

Example 2: Handle custom deep links



protected void startActivityForPushMessage(PushMessage message) {
    String customData = message.getCustomData();
    String deepLink = null;
    if (customData != null) {
        try {
            deepLink = new JSONObject(customData).optString("deep_link", null);
        } catch (JSONException e) {
            Log.e("App", "Failed to parse custom data", e);
        }
    }

    if (deepLink != null && deepLink.startsWith("myapp://")) {
        // Parse and handle custom deep link
        Uri uri = Uri.parse(deepLink);
        String path = uri.getPath(); // e.g., "/product/123"

        // Route based on path
        if (path.startsWith("/product/")) {
            String productId = path.substring("/product/".length());
            openProductScreen(productId);
        } else if (path.equals("/cart")) {
            openCartScreen();
        } else {
            // Unknown deep link - use default
            super.startActivityForPushMessage(message);
        }
    } else {
        // No custom deep link - use default
        super.startActivityForPushMessage(message);
    }
}

Example 3: Add authentication check before opening activity



protected void startActivityForPushMessage(PushMessage message) {
    Context context = getApplicationContext();
    SharedPreferences prefs = context.getSharedPreferences("auth", Context.MODE_PRIVATE);
    boolean isLoggedIn = prefs.getBoolean("is_logged_in", false);

    String screenType = null;
    boolean requiresAuth = false;
    String customData = message.getCustomData();
    if (customData != null) {
        try {
            JSONObject data = new JSONObject(customData);
            screenType = data.optString("screen", null);
            requiresAuth = data.optBoolean("requires_auth", false);
        } catch (JSONException e) {
            Log.e("App", "Failed to parse custom data", e);
        }
    }

    if (requiresAuth && !isLoggedIn) {
        // Redirect to login screen
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("redirect_screen", screenType);
        intent.putExtra("pushMessage", message.toJson().toString());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    } else {
        // User is authenticated or auth not required
        super.startActivityForPushMessage(message);
    }
}

Parameters

message

the push message containing notification data and custom payload used for routing

See also