toJson
Converts this push message to JSON.
Returns the raw push payload as a JSONObject containing all the notification data received from FCM/HMS. This is useful for logging, debugging, sending data to analytics, or when you need to access the complete push payload in JSON format. Example:
protected boolean onMessageReceived(PushMessage message) {
// Log complete push payload for debugging
JSONObject json = message.toJson();
Log.d("App", "Push received: " + json.toString());
// Send to analytics
analytics.logEvent("push_received", new Bundle() {{
putString("payload", json.toString());
putLong("campaign_id", message.getCampaignId());
}});
// Check for specific custom fields
if (json.has("priority_delivery")) {
boolean isPriority = json.optBoolean("priority_delivery");
if (isPriority) {
// Handle high-priority notification
showImmediateNotification(message);
return true;
}
}
return false;
}
// Storing notification history
protected void saveNotificationHistory(PushMessage message) {
JSONObject json = message.toJson();
try {
// Add timestamp
json.put("received_at", System.currentTimeMillis());
// Save to database or file
saveToDatabase(json.toString());
} catch (JSONException e) {
Log.e("App", "Failed to save notification", e);
}
}
Content copied to clipboard
Return
JSONObject containing the complete push notification payload