NotificationFactory
Base class for customizing push notification appearance and behavior.
NotificationFactory allows you to control how push notifications are displayed to users by overriding the notification generation process. You can customize notification icons, colors, sounds, vibration patterns, LED indicators, and even the notification layout itself.
Key Features:
- Custom notification layouts - Create your own notification designs with custom views
- Dynamic content - Modify notification content based on app state or user preferences
- Notification channels - Customize Android 8.0+ notification channel names and descriptions
- Rich media handling - Override how images and other media are displayed in notifications
- Helper methods - Use built-in methods for adding sounds, vibration, LED effects, and auto-cancel behavior
Quick Start:
// 1. Create custom factory
public class CustomNotificationFactory extends NotificationFactory {
public Notification onGenerateNotification(@NonNull PushMessage data) {
// Get notification channel (Android 8.0+)
String channelId = addChannel(data);
// Build notification with custom styling
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification)
.setColor(0xFF6200EE); // Custom brand color
Notification notification = builder.build();
// Add sound, vibration, LED using helper methods
addSound(notification, data.getSound());
addVibration(notification, data.getVibration());
addLED(notification, data.getLed(), data.getLedOnMS(), data.getLedOffMS());
addCancel(notification); // Auto-dismiss when tapped
return notification;
}
}
// 2. Register in AndroidManifest.xml
<application>
<meta-data
android:name="com.pushwoosh.notification_factory"
android:value=".CustomNotificationFactory" />
</application>
Content copied to clipboard
Important Requirements:
- Your factory class MUST be public
- Your factory MUST have a public no-argument constructor
- Application will crash on startup if these requirements are not met
- The onGenerateNotification() method runs on a background thread
Advanced Usage - Custom Notification Channels:
public class CustomNotificationFactory extends NotificationFactory {
public String channelName(String channelName) {
// Customize channel name based on push payload
if (channelName.equals("urgent")) {
return "Urgent Notifications";
}
return "General Notifications";
}
public String channelDescription(String channelName) {
// Provide user-friendly descriptions
return "Important updates about your account";
}
}
Content copied to clipboard
See Also:
- PushwooshNotificationFactory - Default implementation showing best practices
- NotificationServiceExtension - Alternative way to customize notifications before they are displayed
- PushMessage - Contains all push notification data and metadata
See also
Inheritors
Functions
Link copied to clipboard
Creates a notification channel for the push message if it doesn't already exist.
Link copied to clipboard
Adds vibration to the notification.
Link copied to clipboard
Provides a description for a notification channel.
Link copied to clipboard
Customizes the display name of a notification channel.
Link copied to clipboard
Converts HTML-formatted string to styled CharSequence for display in notifications.
Link copied to clipboard
Creates the intent that will be fired when the user taps the notification.
Link copied to clipboard
Creates and configures a notification from push message data.