@@ -16,6 +16,7 @@ user clicks it</li>
1616 <h2>In this document</h2>
1717 <ol>
1818 <li><a href="#Basics">The Basics</a></li>
19+ <li><a href="#HandlingNotifications">Responding to Notifications</a></li>
1920 <li><a href="#ManageYourNotifications">Managing your Notifications</a></li>
2021 <li><a href="#CreateANotification">Creating a Notification</a>
2122 <ol>
@@ -137,6 +138,138 @@ mNotificationManager.notify(HELLO_ID, notification);
137138</ol>
138139
139140
141+ <h2 id="HandlingNotifications">Responding to Notifications</h2>
142+
143+ <p>A central part of the user's experience with a notification revolves around
144+ how it interacts with the application's UI flow. You must implement
145+ this correctly to provide a consistent user experience within your app.</p>
146+
147+ <p>Two typical examples of notifications are provided by Calendar, which can send out
148+ notifications of upcoming events, and Email, which can send out notifications
149+ when new messages arrive. These represent the two recommended patterns for handling
150+ notifications: either launching into an activity that is separate from the
151+ main application, or launching an entirely new instance of the application
152+ showing the appropriate point for the notification.</p>
153+
154+ <p>The following scenario shows how the activity stack should work
155+ in these two typical notification flows, first handling a Calendar notification:
156+ </p>
157+
158+ <ol>
159+ <li>User is creating a new event in Calendar. They realize they
160+ need to copy part of an email message into this event.
161+ </li>
162+ <li>
163+ The user chooses Home > Email.
164+ </li>
165+ <li>
166+ While in Email, they receive a notification from Calendar for an upcoming
167+ meeting.
168+ </li>
169+ <li>
170+ So they choose that notification, which takes them to a
171+ dedicated Calendar activity that displays brief details of the
172+ upcoming meeting.
173+ </li>
174+ <li>
175+ The user has seen enough to know they have a meeting coming up,
176+ so they press the BACK button. They are now returned to Email, which
177+ is where they were when they took the notification.
178+ </li>
179+ </ol>
180+
181+ <p>Handling an Email notification:</p>
182+
183+ <ol>
184+ <li>
185+ The user is currently in Email composing a message, and needs to
186+ check a date in their calendar.
187+ </li>
188+ <li>
189+ The user chooses Home > Calendar.
190+ </li>
191+ <li>
192+ While in Calendar, they receive a notification from Email about a new
193+ message.
194+ </li>
195+ <li>
196+ They select the notification, which brings them to Email with the message
197+ details displayed. This has replaced what they were previously doing
198+ (writing an e-mail), but that message is still saved in their drafts.
199+ </li>
200+ <li>
201+ The user presses BACK once to go to the message list (the typical flow in the
202+ Email app), and press BACK again to return to Calendar as they left it.
203+ </li>
204+ </ol>
205+
206+ <p>In an Email style of notification, the UI launched by the notification
207+ shows the main application in a state representing that notification.
208+ For example, when the Email application comes to the foreground from its
209+ notification, it displays either the conversion list or a specific
210+ conversation depending on whether there are multiple or only one new
211+ email. To achieve this, we want to completely replace whatever current
212+ state the application is in with a new activity stack representing the
213+ new notification state.</p>
214+
215+ <p>The following code illustrates how to show this kind of notification. Of
216+ most interest is the <code>makeMessageIntentStack()</code> method, which constructs
217+ an array of intents representing the app's new activity stack for this state.
218+ (If you are using fragments, you may need to initialize your fragment and
219+ app state so that pressing BACK will switch the UI back to its parent state.)
220+ The core of this is the {@link android.content.Intent#makeRestartActivityTask
221+ Intent.makeRestartActivityTask()} method, which constructs the root activity
222+ of the stack with the appropriate flags, such as
223+ {@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TASK Intent.FLAG_ACTIVITY_CLEAR_TASK}.</p>
224+
225+ {@sample development/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java
226+ app_notification}
227+
228+ <p>In a Calendar style of notification, the UI launched by the notification
229+ is a dedicated activity that is not part of the normal application flow.
230+ For example, when the user receives a Calendar notification, choosing that
231+ notification starts a special activity that displays a list
232+ of upcoming calendar events — this view is available only
233+ from the notification, not through the Calendar's normal user
234+ interface.</p>
235+
236+ <p>The code for posting this type of notification is very straight-forward; it
237+ is like the above, but the {@link android.app.PendingIntent} is for just a single
238+ activity, our dedicated notification activity.</p>
239+
240+ {@sample development/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java
241+ interstitial_notification}
242+
243+ <p>This is not enough, however. Normally Android considers all activities within
244+ an application to be part of that application's UI flow, so simply launching the
245+ activity like this can cause it to be mixed with your normal application back stack
246+ in undesired ways. To make it behave correctly, in the manifest declaration
247+ for the activity the attributes
248+ <code>android:launchMode="singleInstance"</code> and
249+ <code>android:excludeFromRecents="true"</code>
250+ must be set. The full activity declaration for this sample is:</p>
251+
252+ {@sample development/samples/ApiDemos/AndroidManifest.xml interstitial_affinity}
253+
254+ <p>Because of the use of <code>singleInstance</code>, you must be careful about launching
255+ any other activities from this one. These activities will be launched
256+ in their own task, and care must be taken to make sure this interacts
257+ well with the current state of your application's task. This is essentially
258+ the same as switching to the main application as described for the Email style
259+ notification shown before. Given the <code>makeMessageIntentStack()</code>
260+ method previously shown, handling a click here would look something like this:</p>
261+
262+ {@sample development/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageInterstitial.java
263+ app_launch}
264+
265+ <p>If you don't want to use the <code>singleInstance</code> launch mode for
266+ this activity, an alternative approach is to use <code>android:taskAffinity=""</code>.
267+ This tells Android that the activity should not be treated as part of the
268+ main application flow, so it will not get mixed together with that. All of the
269+ other issues discussed here do still apply, though this would allow you to start
270+ additional activities that are part of this notification task instead of switching
271+ to and replacing the main application task.</p>
272+
140273<h2 id="ManageYourNotifications">Managing your Notifications</h2>
141274
142275<p>The {@link android.app.NotificationManager} is a system service that manages all
0 commit comments