onMessageCanceled

protected open fun onMessageCanceled(message: PushMessage)

Callback invoked when a user dismisses or swipes away a notification.

Override this method to handle notification dismissal events. This is useful for tracking user engagement, cleaning up notification state, or updating notification counters. The callback is triggered when the user explicitly dismisses a notification from the notification center (not when it expires or is cleared programmatically). Example:



protected void onMessageCanceled(PushMessage message) {
    String customData = message.getCustomData();
    if (customData != null) {
        try {
            JSONObject data = new JSONObject(customData);

            // Track dismissal in analytics
            String campaignId = data.optString("campaign_id", null);
            Analytics.track("notification_dismissed", campaignId);

            // Clean up related local data
            String notificationId = data.optString("notification_id", null);
            deleteNotificationData(notificationId);
        } catch (JSONException e) {
            Log.e("App", "Failed to parse custom data", e);
        }
    }

    // Update badge count
    SharedPreferences prefs = getApplicationContext()
        .getSharedPreferences("notifications", Context.MODE_PRIVATE);
    int badgeCount = prefs.getInt("badge_count", 0);
    if (badgeCount > 0) {
        prefs.edit().putInt("badge_count", badgeCount - 1).apply();
    }
}

Parameters

message

the push message that was dismissed by the user

See also