Skip to content

Commit ac294ab

Browse files
author
Joe Malin
committed
Revision to Notifications API guide
Change-Id: I52229dfdff3649e03a1fbce91234c8b2a544502a
1 parent c725556 commit ac294ab

File tree

1 file changed

+104
-24
lines changed

1 file changed

+104
-24
lines changed

docs/html/guide/topics/ui/notifiers/notifications.jd

Lines changed: 104 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ page.title=Notifications
1818
<li><a href="#Actions">Notification actions</a></li>
1919
<li><a href="#SimpleNotification">Creating a simple notification</a></li>
2020
<li><a href="#ApplyStyle">Applying a big view style to a notification</a></li>
21+
<li><a href="#Compatibility">Handling compatibility</a></li>
2122
</ol>
2223
</li>
2324
<li><a href="#Managing">Managing Notifications</a>
@@ -91,18 +92,36 @@ page.title=Notifications
9192
</p>
9293
</div>
9394
<p class="note">
94-
<strong>Note:</strong> This guide refers to the
95+
<strong>Note:</strong> Except where noted, this guide refers to the
9596
{@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder} class
9697
in the version 4 <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>.
97-
The class {@link android.app.Notification.Builder Notification.Builder} was added in API
98-
level 11.
98+
The class {@link android.app.Notification.Builder Notification.Builder} was added in Android
99+
3.0.
99100
</p>
100101
<!-- ------------------------------------------------------------------------------------------ -->
101102
<!-- ------------------------------------------------------------------------------------------ -->
102103
<h2 id="NotificationUI">Notification Display Elements</h2>
103104
<p>
104-
Notifications in the notification drawer appear in two main visual styles, normal view and
105-
big view.
105+
Notifications in the notification drawer can appear in one of two visual styles, depending on
106+
the version and the state of the drawer:
107+
</p>
108+
<dl>
109+
<dt>
110+
Normal view
111+
</dt>
112+
<dd>
113+
The standard view of the notifications in the notification drawer.
114+
</dd>
115+
<dt>
116+
Big view
117+
</dt>
118+
<dd>
119+
A large view that's visible when the notification is expanded. Big view is part of the
120+
expanded notification feature available as of Android 4.1.
121+
</dd>
122+
</dl>
123+
<p>
124+
These styles are described in the following sections.
106125
</p>
107126
<!-- ------------------------------------------------------------------------------------------ -->
108127
<h3 id="NormalNotify">Normal view</h3>
@@ -139,7 +158,7 @@ page.title=Notifications
139158
<p>
140159
A notification's big view appears only when the notification is expanded, which happens when the
141160
notification is at the top of the notification drawer, or when the user expands the
142-
notification with a gesture.
161+
notification with a gesture. Expanded notifications are available starting with Android 4.1.
143162
</p>
144163
<p>
145164
The following screenshot shows an inbox-style notification:
@@ -246,33 +265,35 @@ page.title=Notifications
246265
</p>
247266
<p>
248267
A notification can provide multiple actions. You should always define the action that's
249-
triggered when the user touches the notification; usually this action opens an
268+
triggered when the user clicks the notification; usually this action opens an
250269
{@link android.app.Activity} in your application. You can also add buttons to the notification
251270
that perform additional actions such as snoozing an alarm or responding immediately to a text
252-
message.
271+
message; this feature is available as of Android 4.1. If you use additional action buttons, you
272+
must also make their functionality available in an {@link android.app.Activity} in your app; see
273+
the section <a href="#Compatibility">Handling compatibility</a> for more details.
253274
</p>
254275
<p>
255276
Inside a {@link android.app.Notification}, the action itself is defined by a
256277
{@link android.app.PendingIntent} containing an {@link android.content.Intent} that starts
257278
an {@link android.app.Activity} in your application. To associate the
258279
{@link android.app.PendingIntent} with a gesture, call the appropriate method of
259280
{@link android.support.v4.app.NotificationCompat.Builder}. For example, if you want to start
260-
{@link android.app.Activity} when the user touches the notification text in
281+
{@link android.app.Activity} when the user clicks the notification text in
261282
the notification drawer, you add the {@link android.app.PendingIntent} by calling
262283
{@link android.support.v4.app.NotificationCompat.Builder#setContentIntent setContentIntent()}.
263284
</p>
264285
<p>
265-
Starting an {@link android.app.Activity} when the user touches the notification is the most
286+
Starting an {@link android.app.Activity} when the user clicks the notification is the most
266287
common action scenario. You can also start an {@link android.app.Activity} when the user
267-
dismisses an {@link android.app.Activity}, and you can start an {@link android.app.Activity}
268-
from an action button. To learn more, read the reference guide for
288+
dismisses an {@link android.app.Activity}. In Android 4.1 and later, you can start an
289+
{@link android.app.Activity} from an action button. To learn more, read the reference guide for
269290
{@link android.support.v4.app.NotificationCompat.Builder}.
270291
</p>
271292
<!-- ------------------------------------------------------------------------------------------ -->
272293
<h3 id="SimpleNotification">Creating a simple notification</h3>
273294
<p>
274295
The following snippet illustrates a simple notification that specifies an activity to open when
275-
the user touches the notification. Notice that the code creates a
296+
the user clicks the notification. Notice that the code creates a
276297
{@link android.support.v4.app.TaskStackBuilder} object and uses it to create the
277298
{@link android.app.PendingIntent} for the action. This pattern is explained in more detail
278299
in the section <a href="#NotificationResponse">
@@ -316,6 +337,11 @@ mNotificationManager.notify(mId, mBuilder.build());
316337
you want. Next, call {@link android.support.v4.app.NotificationCompat.Builder#setStyle
317338
Builder.setStyle()} with a big view style object as its argument.
318339
</p>
340+
<p>
341+
Remember that expanded notifications are not available on platforms prior to Android 4.1. To
342+
learn how to handle notifications for Android 4.1 and for earlier platforms, read the
343+
section <a href="#Compatibility">Handling compatibility</a>.
344+
</p>
319345
<p>
320346
For example, the following code snippet demonstrates how to alter the notification created
321347
in the previous snippet to use the Inbox big view style:
@@ -341,6 +367,47 @@ mBuilder.setStyle(inBoxStyle);
341367
...
342368
// Issue the notification here.
343369
</pre>
370+
<h3 id="Compatibility">Handling compatibility</h3>
371+
<p>
372+
Not all notification features are available for a particular version, even though
373+
the methods to set them are in the support library class
374+
{@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder}.
375+
For example, action buttons, which depend on expanded notifications, only appear on Android
376+
4.1 and higher, because expanded notifications themselves are only available on
377+
Android 4.1 and higher.
378+
</p>
379+
<p>
380+
To ensure the best compatibility, create notifications with
381+
{@link android.support.v4.app.NotificationCompat NotificationCompat} and its subclasses,
382+
particularly {@link android.support.v4.app.NotificationCompat.Builder
383+
NotificationCompat.Builder}. In addition, follow this process when you implement a notification:
384+
</p>
385+
<ol>
386+
<li>
387+
Provide all of the notification's functionality to all users, regardless of the version
388+
they're using. To do this, verify that all of the functionality is available from an
389+
{@link android.app.Activity} in your app. You may want to add a new
390+
{@link android.app.Activity} to do this.
391+
<p>
392+
For example, if you want to use
393+
{@link android.support.v4.app.NotificationCompat.Builder#addAction addAction()} to
394+
provide a control that stops and starts media playback, first implement this
395+
control in an {@link android.app.Activity} in your app.
396+
</p>
397+
</li>
398+
<li>
399+
Ensure that all users can get to the functionality in the {@link android.app.Activity},
400+
by having it start when users click the notification. To do this,
401+
create a {@link android.app.PendingIntent} for the {@link android.app.Activity}. Call
402+
{@link android.support.v4.app.NotificationCompat.Builder#setContentIntent
403+
setContentIntent()} to add the {@link android.app.PendingIntent} to the notification.
404+
</li>
405+
<li>
406+
Now add the expanded notification features you want to use to the notification. Remember
407+
that any functionality you add also has to be available in the {@link android.app.Activity}
408+
that starts when users click the notification.
409+
</li>
410+
</ol>
344411
<!-- ------------------------------------------------------------------------------------------ -->
345412
<!-- ------------------------------------------------------------------------------------------ -->
346413
<h2 id="Managing">Managing Notifications</h2>
@@ -355,6 +422,10 @@ mBuilder.setStyle(inBoxStyle);
355422
"stacking" the notification; it's described in more detail in the
356423
<a href="{@docRoot}design/patterns/notifications.html">Notifications</a> Design guide.
357424
</p>
425+
<p class="note">
426+
<strong>Note:</strong> This Gmail feature requires the "inbox" big view style, which is
427+
part of the expanded notification feature available starting in Android 4.1.
428+
</p>
358429
<p>
359430
The following section describes how to update notifications and also how to remove them.
360431
</p>
@@ -417,7 +488,7 @@ numMessages = 0;
417488
the notification can be cleared).
418489
</li>
419490
<li>
420-
The user touches the notification, and you called
491+
The user clicks the notification, and you called
421492
{@link android.support.v4.app.NotificationCompat.Builder#setAutoCancel setAutoCancel()} when
422493
you created the notification.
423494
</li>
@@ -452,7 +523,7 @@ numMessages = 0;
452523
start a fresh task, and provide the {@link android.app.PendingIntent} with a back stack
453524
that reproduces the application's normal <i>Back</i> behavior.
454525
<p>
455-
Notifications from the Gmail app demonstrate this. When you touch a notification for
526+
Notifications from the Gmail app demonstrate this. When you click a notification for
456527
a single email message, you see the message itself. Touching <b>Back</b> takes you
457528
backwards through Gmail to the Home screen, just as if you had entered Gmail from the
458529
Home screen rather than entering it from a notification.
@@ -489,7 +560,7 @@ numMessages = 0;
489560
Define your application's {@link android.app.Activity} hierarchy in the manifest.
490561
<ol style="list-style-type: lower-alpha;">
491562
<li>
492-
Add support for API versions 15 and earlier. To do this, specify the parent of the
563+
Add support for Android 4.0.3 and earlier. To do this, specify the parent of the
493564
{@link android.app.Activity} you're starting by adding a
494565
<code><a href="{@docRoot}guide/topics/manifest/meta-data-element.html">&lt;meta-data&gt;</a></code>
495566
element as the child of the
@@ -507,7 +578,7 @@ numMessages = 0;
507578
</p>
508579
</li>
509580
<li>
510-
Also add support for API versions 16 and later. To do this, add the
581+
Also add support for Android 4.1 and later. To do this, add the
511582
<code><a href="{@docRoot}guide/topics/manifest/activity-element.html#parent">android:parentActivityName</a></code>
512583
attribute to the
513584
<code><a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></code>
@@ -738,9 +809,14 @@ mNotificationManager.notify(id, builder.build());
738809
{@link android.widget.ProgressBar} class.
739810
</p>
740811
<p>
741-
To use a progress indicator, call
742-
{@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress()}. The
743-
determinate and indeterminate forms are described in the following sections.
812+
To use a progress indicator on platforms starting with Android 4.0, call
813+
{@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress()}. For
814+
previous versions, you must create your own custom notification layout that
815+
includes a {@link android.widget.ProgressBar} view.
816+
</p>
817+
<p>
818+
The following sections describe how to display progress in a notification using
819+
{@link android.support.v4.app.NotificationCompat.Builder#setProgress setProgress()}.
744820
</p>
745821
<!-- ------------------------------------------------------------------------------------------ -->
746822
<h3 id="FixedProgress">Displaying a fixed-duration progress indicator</h3>
@@ -871,6 +947,10 @@ mNotifyManager.notify(0, mBuilder.build());
871947
Custom layout notifications are similar to normal notifications, but they're based on a
872948
{@link android.widget.RemoteViews} defined in a XML layout file.
873949
</p>
950+
<p>
951+
The height available for a custom notification layout depends on the notification view. Normal
952+
view layouts are limited to 64 dp, and expanded view layouts are limited to 256 dp.
953+
</p>
874954
<p>
875955
To define a custom notification layout, start by instantiating a
876956
{@link android.widget.RemoteViews} object that inflates an XML layout file. Then,
@@ -911,8 +991,8 @@ mNotifyManager.notify(0, mBuilder.build());
911991
<h4>Using style resources for custom notification text</h4>
912992
<p>
913993
Always use style resources for the text of a custom notification. The background color of the
914-
notification can vary across different devices and platform versions, and using style resources
915-
helps you account for this. Starting in API level 9, the system defined a style for the
916-
standard notification layout text. If you use the same style in applications that target API
917-
level 9 or higher, you'll ensure that your text is visible against the display background.
994+
notification can vary across different devices and versions, and using style resources
995+
helps you account for this. Starting in Android 2.3, the system defined a style for the
996+
standard notification layout text. If you use the same style in applications that target Android
997+
2.3 or higher, you'll ensure that your text is visible against the display background.
918998
</p>

0 commit comments

Comments
 (0)