NotificationFactory

abstract class 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>

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";
      }
  }

See Also:

See also

Inheritors

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
protected val applicationContext: Context

Functions

Link copied to clipboard
protected fun addCancel(notification: Notification)
Makes the notification automatically dismiss when the user taps it.
Link copied to clipboard
protected open fun addChannel(pushMessage: PushMessage): String
Creates a notification channel for the push message if it doesn't already exist.
Link copied to clipboard
protected fun addLED(notification: Notification, color: Integer, ledOnMs: Int, ledOffMs: Int)
Adds LED blinking effect to the notification.
Link copied to clipboard
protected fun addSound(notification: Notification, sound: String)
Adds a sound to the notification.
Link copied to clipboard
protected fun addVibration(notification: Notification, vibration: Boolean)
Adds vibration to the notification.
Link copied to clipboard
open fun channelDescription(channelName: String): String
Provides a description for a notification channel.
Link copied to clipboard
open fun channelName(channelName: String): String
Customizes the display name of a notification channel.
Link copied to clipboard
protected fun getContentFromHtml(content: String): CharSequence
Converts HTML-formatted string to styled CharSequence for display in notifications.
Link copied to clipboard
open fun getNotificationIntent(data: PushMessage): Intent
Creates the intent that will be fired when the user taps the notification.
Link copied to clipboard
abstract fun onGenerateNotification(data: PushMessage): Notification
Creates and configures a notification from push message data.