addChannel
Creates a notification channel for the push message if it doesn't already exist.
On Android 8.0 (API 26) and higher, all notifications must be assigned to a channel. This method automatically creates the appropriate channel based on the push message payload's "pw_channel" attribute, or creates a default channel if no channel is specified.
Channel settings (name, description, sound, vibration, LED) are configured when the channel is first created and cannot be changed programmatically afterwards. Users can modify these settings through the system notification settings UI.
Channel Customization: Override channelName and channelDescription to customize channel display names and descriptions.
Android 7.1 and lower: This method returns a channel ID for compatibility but doesn't create an actual channel (channels don't exist on these versions). Example - Basic usage:
public Notification onGenerateNotification(@NonNull PushMessage data) {
// Create channel (required for Android 8.0+)
String channelId = addChannel(data);
// Use channel ID when building notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification);
return builder.build();
}
public class CustomNotificationFactory extends NotificationFactory {
public String channelName(String channelName) {
// Map technical IDs to user-friendly names
if ("promo".equals(channelName)) {
return "Promotions";
} else if ("orders".equals(channelName)) {
return "Order Updates";
}
return "Notifications";
}
public Notification onGenerateNotification(@NonNull PushMessage data) {
// Channel created with custom name from channelName() method
String channelId = addChannel(data);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId)
.setContentTitle(data.getHeader())
.setContentText(data.getMessage())
.setSmallIcon(R.drawable.ic_notification);
return builder.build();
}
}
Return
Channel ID to use when building the notification. On Android 7.1 and lower, returnsa placeholder ID (no actual channel is created).
Parameters
Push message containing channel information in "pw_channel" attribute.If not specified, a default channel will be created.