NotificationServiceExtension

Allows customization of push notification behavior before display and when opened.

NotificationServiceExtension provides hook methods to intercept and modify push notification processing at key moments: when notifications are received, opened, canceled, or when notification groups are opened. This is the primary extension point for customizing how your app handles push notifications.

Key Features:

  • Foreground Handling - Prevent notification display when app is in foreground
  • Custom Launch Behavior - Control which activity opens when notification is tapped
  • Deep Link Control - Override default URL/deep link handling
  • Lifecycle Callbacks - Track notification open, cancel, and group open events
  • Context Access - Access application context for custom logic

Setup:

1. Create a public class extending NotificationServiceExtension with a public no-args constructor:


public class MyNotificationService extends NotificationServiceExtension {
    
    protected boolean onMessageReceived(PushMessage message) {
        // Return true to suppress notification when app is in foreground
        if (isAppOnForeground()) {
            // Handle notification in-app (show dialog, update UI, etc.)
            showInAppAlert(message.getMessage());
            return true;
        }
        return false;
    }

    
    protected void onMessageOpened(PushMessage message) {
        // Track notification open event
        String campaignId = message.getCustomData().getString("campaign_id");
        Analytics.track("notification_opened", campaignId);
    }
}

2. Register your extension in AndroidManifest.xml:


<application>
    <meta-data
        android:name="com.pushwoosh.notification_service_extension"
        android:value="com.your.package.MyNotificationService" />
</application>

Common Use Cases:Example 1: Suppress notifications when app is in foreground



protected boolean onMessageReceived(PushMessage message) {
    if (isAppOnForeground()) {
        // Show in-app notification instead
        Context context = getApplicationContext();
        Toast.makeText(context,
            message.getMessage(),
            Toast.LENGTH_LONG).show();
        return true; // Notification won't be displayed
    }
    return false; // Show notification normally
}

Example 2: Launch specific activity 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 ("cart".equals(screenType)) {
        // Open shopping cart
        intent = new Intent(context, CartActivity.class);
    } else {
        // Default behavior
        super.startActivityForPushMessage(message);
        return;
    }

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(intent);
}

Example 3: Handle notification cancel events



protected void onMessageCanceled(PushMessage message) {
    // User dismissed notification - track in analytics
    String campaignId = message.getCustomData().getString("campaign_id");
    Analytics.track("notification_dismissed", campaignId);

    // Update notification count
    SharedPreferences prefs = getApplicationContext()
        .getSharedPreferences("stats", Context.MODE_PRIVATE);
    int dismissCount = prefs.getInt("dismiss_count", 0);
    prefs.edit().putInt("dismiss_count", dismissCount + 1).apply();
}

Example 4: Override URL handling



protected boolean preHandleNotificationsWithUrl() {
    // Return false to disable automatic URL/deep link handling
    // You must handle URLs manually in startActivityForPushMessage()
    return false;
}


protected void startActivityForPushMessage(PushMessage message) {
    // Custom URL handling logic
    String url = message.getLink();
    if (url != null && url.startsWith("myapp://")) {
        // Handle custom deep link
        handleDeepLink(url);
    } else {
        super.startActivityForPushMessage(message);
    }
}

Important Notes:

  • Your extension class must be public and have a public no-argument constructor
  • Application will crash on startup if these requirements are not met
  • onMessageReceived runs on a background thread - use getApplicationContext safely
  • startActivityForPushMessage runs on the main thread
  • Returning true from onMessageReceived suppresses notification display
  • Only one NotificationServiceExtension can be registered per application

See also

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
protected open val applicationContext: Context

Functions

Link copied to clipboard
fun handleMessage(pushBundle: Bundle)
Internal method that handles incoming push notification messages.
Link copied to clipboard
fun handleNotification(pushBundle: Bundle)
Internal method that handles notification open events.
Link copied to clipboard
fun handleNotificationCanceled(pushBundle: Bundle)
Internal method that handles notification dismissal events.
Link copied to clipboard
Internal method that handles notification group open events.
Link copied to clipboard
protected open fun isAppOnForeground(): Boolean
Checks whether the application is currently running in the foreground.
Link copied to clipboard
protected open fun onMessageCanceled(message: PushMessage)
Callback invoked when a user dismisses or swipes away a notification.
Link copied to clipboard
protected open fun onMessageOpened(message: PushMessage)
Callback invoked when a user taps on a notification.
Link copied to clipboard
protected open fun onMessageReceived(data: PushMessage): Boolean
Callback invoked when a push notification is received, before it is displayed.
Link copied to clipboard
protected open fun onMessagesGroupOpened(messages: List<PushMessage>)
Callback invoked when a user taps on a grouped notification (notification stack).
Link copied to clipboard
Controls whether Pushwoosh should automatically handle notifications containing URLs or deep links.
Link copied to clipboard
protected open fun startActivityForPushMessage(message: PushMessage)
Callback invoked to launch an activity when a notification is opened.