java.lang.Object | ||
↳ | com.urbanairship.push.notifications.NotificationFactory | |
↳ | com.urbanairship.push.notifications.CustomLayoutNotificationFactory |
A notification factory that allows the use of layout XML and a custom notification sound.
A layout resource is required. It can include a ImageView
It must include an ImageView
for an icon, a
TextView
for the alert subject or title, and a TextView
for
the alert message. Each of these is required, but if your layout does not make use of one or
more of the items, set the visibility to gone
(android:visibility="gone"
).
A sample layout.xml file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingTop="2dip"
android:layout_alignParentTop="true"
android:layout_height="fill_parent">
<ImageView android:id="@+id/icon"
android:src="@drawable/icon_1"
android:layout_marginRight="4dip"
android:layout_marginLeft="5dip"
android:layout_width="100dip"
android:layout_height="100dip" />
<!-- The custom notification requires a subject field.
To accommodate multiple lines in this layout this
field is hidden. Visibility is set to gone. -->
<TextView android:id="@+id/subject"
android:text="Subject"
android:layout_alignTop="@+id/icon"
android:layout_toRightOf="@+id/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxLines="1" android:visibility="gone"/>
<!-- The message block. Standard text size is 14dip
but is reduced here to maximize content. -->
<TextView android:id="@+id/message"
android:textSize="12dip"
android:textColor="#FF000000"
android:text="Message"
android:maxLines="4"
android:layout_marginTop="0dip"
android:layout_marginRight="2dip"
android:layout_marginLeft="0dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:layout_width="wrap_content" />
</RelativeLayout>
Note: If you are planning on targeting Honeycomb devices (and beyond), the above layout will not display
properly, as it will show black text on the default black background that Honeycomb and above uses for notifications.
Instead of explicitly setting a light background color, which may look out of place across multiple devices,
you should set the android:textAppearance
property on your TextViews to
@android:style/TextAppearance.StatusBar.EventContent.Title
or
@android:style/TextAppearance.StatusBar.EventContent
, which were introduced in API level 9, to ensure
that your layout will adapt gracefully as the platform changes.
Because these styles are not available prior to API level 9, if you intend to make your app backwards compatible with prior API levels, you can do so by building for the newer API but set an older minimum SDK setting for your app, and including the level 9 compatible layout in its own resource directory, called "layout-v9". See the Push Sample application for a concrete example.
Below is a sample Honeycomb-compatible layout, similar to the one above:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingTop="0dip"
android:layout_alignParentTop="true"
android:layout_height="fill_parent">
<ImageView android:id="@+id/icon"
android:src="@drawable/icon"
android:layout_marginRight="10dip"
android:layout_marginLeft="0dip"
android:layout_width="65dip"
android:layout_height="65dip" />
<!-- The custom notification requires a subject field.
To accommodate multiple lines in this layout this
field is hidden. Visibility is set to gone. -->
<TextView android:id="@+id/subject"
android:text="Subject"
android:textAppearance="@android:style/TextAppearance.StatusBar.EventContent.Title"
android:layout_alignTop="@+id/icon"
android:layout_toRightOf="@+id/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxLines="1" android:visibility="gone"/>
<!-- The message block. Standard text size is 14dip
but is reduced here to maximize content. -->
<TextView android:id="@+id/message"
android:textSize="12dip"
android:textAppearance="@android:style/TextAppearance.StatusBar.EventContent"
android:text="Message"
android:maxLines="4"
android:layout_marginTop="0dip"
android:layout_marginRight="2dip"
android:layout_marginLeft="0dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:layout_width="wrap_content" />
</RelativeLayout>
And here is a sample implementation:
CustomLayoutNotificationFactory nf = new CustomLayoutNotificationFactory(this);
nf.layout = R.layout.notification_layout; // The layout resource to use
nf.layoutIconDrawableId = R.drawable.notification_icon; // The icon you want to display
nf.layoutIconId = R.id.icon; // The icon's layout 'id'
nf.layoutSubjectId = R.id.subject; // The id for the 'subject' field
nf.layoutMessageId = R.id.message; // The id for the 'message' field
//set this ID to a value > 0 if you want a new notification to replace the previous one
nf.constantNotificationId = 100;
//set this if you want a custom sound to play
nf.soundUri = Uri.parse("android.resource://"+this.getPackageName()+"/" +R.raw.notification_mp3);
// Set the factory
UAirship.shared().getPushManager().setNotificationBuilder(nf);
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
public int | constantNotificationId | An optional constant notification ID. | |||||||||
public int | layout | The layout resource. | |||||||||
public int | layoutIconDrawableId | The icon drawable to display in the custom layout. | |||||||||
public int | layoutIconId | The layout id for the icon. | |||||||||
public int | layoutMessageId | The layout id for the message TextView . |
|||||||||
public int | layoutSubjectId | The layout id for the subject TextView . |
|||||||||
public Uri | soundUri | An optional sound URI. | |||||||||
public int | statusBarIconDrawableId | The icon drawable to display in the status bar. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
CustomLayoutNotificationFactory(Context context) |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Notification |
createNotification(PushMessage pushMessage, int notificationId)
Creates a
Notification for an incoming push message. | ||||||||||
int |
getNextId(PushMessage pushMessage)
Creates a notification ID based on the message and payload.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
An optional constant notification ID.
By default, this builder uses unique, incremented notification IDs. That ID scheme ensures that all notifications are displayed until dismissed by a user. However, if a single notification scheme is desired where each new notification replaces the previous one, set this to a value greater than zero. IfconstantNotificationId <= 0
, the standard incrementing
behavior will be used.
The layout resource.
For example, if the layout isnotification_layout.xml
, use R.layout.notification_layout
.
The icon drawable to display in the custom layout.
For example:R.drawable.notification_icon
The layout id for the icon.
For example:R.id.icon
. This field will be populated with the icon drawable.
The layout id for the message TextView
.
R.id.message
. This field will be populated with the alert message.
The layout id for the subject TextView
.
R.id.subject
. This field will be populated with the application name.
An optional sound URI.
If notnull
, the sound at this URI will be played
when a notification is received.
The icon drawable to display in the status bar.
Creates a Notification
for an incoming push message.
BaseIntentReceiver
. When the notification is opened
it will call onNotificationOpened(Context, PushMessage, int)
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.pushMessage | The push message. |
---|---|
notificationId | The notification ID. |
null
if no notification is desired.
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).pushMessage | The push message. |
---|