startActivityForPushMessage
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:
- Activity with intent filter action:
{applicationId}.MESSAGE - 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 screenType = message.getCustomData().getString("screen");
Intent intent;
if ("product".equals(screenType)) {
// Open product details
intent = new Intent(context, ProductActivity.class);
String productId = message.getCustomData().getString("product_id");
intent.putExtra("productId", productId);
} else if ("order".equals(screenType)) {
// Open order details
intent = new Intent(context, OrderActivity.class);
String orderId = message.getCustomData().getString("order_id");
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);
}
Example 2: Handle custom deep links
protected void startActivityForPushMessage(PushMessage message) {
String deepLink = message.getCustomData().getString("deep_link");
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 = message.getCustomData().getString("screen");
boolean requiresAuth = message.getCustomData().getBoolean("requires_auth", false);
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
the push message containing notification data and custom payload used for routing