public class

NotificationFactory

extends Object
java.lang.Object
   ↳ com.urbanairship.push.notifications.NotificationFactory
Known Direct Subclasses

Class Overview

Notification factory that provides a pathway for customizing the display of push notifications in the Android NotificationManager.

DefaultNotificationFactory is used by default and applies the big text style. For custom layouts, see CustomLayoutNotificationFactory.

Summary

Constants
String DEFAULT_NOTIFICATION_CHANNEL
int TAG_NOTIFICATION_ID Default Notification ID when the PushMessage defines a notification tag.
Public Constructors
NotificationFactory(Context context)
Default constructor.
Public Methods
Notification createNotification(PushMessage message, int notificationId)
Creates a Notification for an incoming push message.
int getColor()
Get the accent color used in the notification.
int getConstantNotificationId()
Get the constant notification ID.
int getLargeIcon()
Get the large icon used in the notification layout.
int getNextId(PushMessage pushMessage)
Creates a notification ID based on the message and payload.
String getNotificationChannel()
Gets the default notification channel.
int getNotificationDefaultOptions()
Gets the default notification options.
int getSmallIconId()
Get the small icon used in the notification layout.
Uri getSound()
Get the sound played when the notification arrives.
int getTitleId()
Get the title used in the notification layout.
boolean requiresLongRunningTask(PushMessage message)
Checks if the push message requires a long running task.
void setColor(int accentColor)
Set the accent color used in the notification.
NotificationFactory setConstantNotificationId(int id)
Set the optional constant notification ID.
void setLargeIcon(int largeIcon)
Set the large icon used in the notification layout.
void setNotificationChannel(String channel)
Sets the default notification channel.
void setNotificationDefaultOptions(int defaults)
Sets the default notification options.
void setSmallIconId(int smallIconId)
Set the small icon used in the notification layout.
void setSound(Uri sound)
Set the sound played when the notification arrives.
void setTitleId(int titleId)
Set the title used in the notification layout.
Protected Methods
NotificationCompat.Builder createNotificationBuilder(PushMessage message, int notificationId, NotificationCompat.Style defaultStyle)
Creates a NotificationCompat.Builder with the default settings applied.
Context getContext()
Gets application context.
String getTitle(PushMessage message)
Gets the default title for the notification.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String DEFAULT_NOTIFICATION_CHANNEL

Constant Value: "com.urbanairship.default"

public static final int TAG_NOTIFICATION_ID

Default Notification ID when the PushMessage defines a notification tag.

Constant Value: 100 (0x00000064)

Public Constructors

public NotificationFactory (Context context)

Default constructor.

Parameters
context The application context.

Public Methods

public Notification createNotification (PushMessage message, int notificationId)

Creates a Notification for an incoming push message.

In order to handle notification opens, the application should register a broadcast receiver that extends AirshipReceiver. When the notification is opened it will call onNotificationOpened(Context, AirshipReceiver.NotificationInfo) giving the application a chance to handle the notification open. If the broadcast receiver is not registered, or false is returned, an open will be handled by either starting the launcher activity or by sending the notification's content intent if it is present.

Parameters
message The push message.
notificationId The notification ID.
Returns
  • The notification to display, or null if no notification is desired.

public int getColor ()

Get the accent color used in the notification.

Returns
  • The accent color as an int.

public int getConstantNotificationId ()

Get the constant notification ID.

Returns
  • The constant notification ID as an int.

public int getLargeIcon ()

Get the large icon used in the notification layout.

Returns
  • The large icon ID as a int.

public int getNextId (PushMessage pushMessage)

Creates a notification ID based on the message and payload.

This method could return a constant (to always replace the existing ID) or a payload/message specific ID (to replace in cases where there are duplicates, for example) or a random/sequential (to always add a new notification).

The default behavior returns TAG_NOTIFICATION_ID if the push message contains a tag (see getNotificationTag()). Otherwise it will either return getConstantNotificationId() if the constant notification id > 0, or it will return a random ID generated from nextID().

Parameters
pushMessage The push message.
Returns
  • An integer ID for the next notification.

public String getNotificationChannel ()

Gets the default notification channel.

Returns
  • The default notification channel.

public int getNotificationDefaultOptions ()

Gets the default notification options.

Returns
  • The default notification options.

public int getSmallIconId ()

Get the small icon used in the notification layout.

Returns
  • The small icon ID as an int.

public Uri getSound ()

Get the sound played when the notification arrives.

Returns
  • The sound as a Uri.

public int getTitleId ()

Get the title used in the notification layout.

Returns
  • The title as an int.

public boolean requiresLongRunningTask (PushMessage message)

Checks if the push message requires a long running task. If true, the push message will be scheduled to process at a later time when the app has more background time. If false, the app has approximately 10 seconds to post the notification in createNotification(PushMessage, int) and getNextId(PushMessage).

Apps that return false are highly encouraged to add RECEIVE_BOOT_COMPLETED so the push message will persist between device reboots.

Parameters
message The push message.
Returns
  • true to require long running task, otherwise false.

public void setColor (int accentColor)

Set the accent color used in the notification.

Parameters
accentColor The accent color of the main notification icon.

public NotificationFactory setConstantNotificationId (int id)

Set the optional constant notification ID.

Only values greater than 0 will be used by default. Any negative value will be considered invalid and the constant notification ID will be ignored.

By default, the constant notification ID will be used if the push message does not contain a tag. In that case, TAG_NOTIFICATION_ID will be used instead.

Parameters
id The integer ID as an int.

public void setLargeIcon (int largeIcon)

Set the large icon used in the notification layout.

Parameters
largeIcon The large icon ID as an int.

public void setNotificationChannel (String channel)

Sets the default notification channel.

Parameters
channel THe default notification channel.

public void setNotificationDefaultOptions (int defaults)

Sets the default notification options. Defaults to NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE.

Parameters
defaults The default options.

public void setSmallIconId (int smallIconId)

Set the small icon used in the notification layout.

Parameters
smallIconId The small icon ID as an int.

public void setSound (Uri sound)

Set the sound played when the notification arrives.

Parameters
sound The sound as a Uri.

public void setTitleId (int titleId)

Set the title used in the notification layout.

Parameters
titleId The title as an int. A value of -1 will not display a title. A value of 0 will display the application name as the title. A string resource ID will display the specified string as the title.

Protected Methods

protected NotificationCompat.Builder createNotificationBuilder (PushMessage message, int notificationId, NotificationCompat.Style defaultStyle)

Creates a NotificationCompat.Builder with the default settings applied.

Parameters
message The PushMessage.
notificationId The notification id.
defaultStyle The default notification style.
Returns
  • A NotificationCompat.Builder.

protected Context getContext ()

Gets application context.

Returns
  • The application context.

protected String getTitle (PushMessage message)

Gets the default title for the notification. If the getTitleId() is 0, the application label will be used, if greater than 0 the string will be fetched from the resources, and if negative an empty String

Returns
  • The default notification title.