onGenerateNotification
Creates and configures a notification from push message data.
This is the main method you must implement to customize notification appearance. The SDK calls this method on a background thread when a push notification arrives. You should build and return a fully configured Notification object, or return null to prevent the notification from being displayed.
Thread Safety: This method is called on a worker thread, so it's safe to perform network requests or other blocking operations (e.g., downloading notification images). Example - Basic custom notification:
public Notification onGenerateNotification(@NonNull PushMessage data) {
// Create notification channel (required for Android 8.0+)
String channelId = addChannel(data);
// Build notification with standard Android APIs
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification);
Notification notification = builder.build();
// Apply notification behaviors using helper methods
addSound(notification, data.getSound());
addVibration(notification, data.getVibration());
addCancel(notification); // Dismiss when user taps
return notification;
}
Content copied to clipboard
public Notification onGenerateNotification(@NonNull PushMessage data) {
String channelId = addChannel(data);
// Load large icon from URL (safe - running on worker thread)
Bitmap icon = null;
String iconUrl = data.getLargeIconUrl();
if (iconUrl != null) {
try {
icon = Glide.with(getApplicationContext())
.asBitmap()
.load(iconUrl)
.submit(256, 256)
.get();
} catch (Exception e) {
Log.e("CustomFactory", "Failed to load icon", e);
}
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(icon)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Notification notification = builder.build();
addSound(notification, data.getSound());
addVibration(notification, data.getVibration());
addCancel(notification);
return notification;
}
Content copied to clipboard
public Notification onGenerateNotification(@NonNull PushMessage data) {
// Check custom data to decide whether to show notification
String customData = data.getCustomData();
if (customData != null && customData.contains("silent")) {
return null; // Don't show notification
}
// Check app settings
SharedPreferences prefs = getApplicationContext()
.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
boolean notificationsEnabled = prefs.getBoolean("notifications_enabled", true);
if (!notificationsEnabled) {
return null; // User disabled notifications in app settings
}
// Show notification normally
String channelId = addChannel(data);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification);
Notification notification = builder.build();
addCancel(notification);
return notification;
}
Content copied to clipboard
Return
Configured notification to display, or null to suppress the notification
Parameters
data
Push notification data containing message, title, icons, sounds, and custom payload