• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=通知
2page.tags=notifications
3helpoutsWidget=true
4page.image=/preview/images/notifications-card.png
5
6trainingnavtop=true
7
8@jd:body
9
10<div id="qv-wrapper">
11<div id="qv">
12
13<!-- table of contents -->
14<h2>本文件包括</h2>
15<ol>
16  <li><a href="#direct">直接回覆</a></li>
17  <li><a href="#bundle">整合式通知</a></li>
18  <li><a href="#custom">自訂檢視</a></li>
19</ol>
20
21</div>
22</div>
23
24<p>Android N 引進數個新 API,允許應用程式張貼相當顯眼且互動式的通知。
25</p>
26
27<p>Android N 擴充現有的 {@link android.support.v4.app.RemoteInput}
28通知 API,支援在手機上內嵌回覆。此功能允許使用者從通知欄快速回應,而不必造訪您的應用程式。
29</p>
30
31<p>
32  Android N 也允許您將類似的通知結合成單一通知。
33Android N 使用現有的 {@link
34  android.support.v4.app.NotificationCompat.Builder#setGroup
35  NotificationCompat.Builder.setGroup()} 方法來實現此目標。使用者能擴充每個通知,並可個別從通知欄執行動作,例如回覆和關閉每一個通知。
36
37
38</p>
39
40<p>最後,Android N 還新增 API 讓您在應用程式的自訂通知檢視中利用系統的裝飾。
41這些 API 可協助確保通知檢視和標準範本的呈現方式一致。
42
43</p>
44
45<p>本文件將強調說明一些重要變更,您應該在應用程式中使用新的通知功能時納入考量。
46</p>
47
48<h2 id="direct">直接回覆</h2>
49
50<p>使用 Android N 中的直接回覆功能,使用者可直接在通知介面內快速回應文字訊息或更新工作清單。
51
52在手持式裝置上,內嵌回覆動作看起來就像是通知附加的額外按鈕。
53當使用者透過鍵盤回覆時,系統會在您為通知動作指定的意圖附加文字回應,然後將意圖傳送給您的手持裝置應用程式。
54
55
56
57
58
59<img id="fig-reply-button" src="{@docRoot}preview/images/inline-reply.png" srcset="{@docRoot}preview/images/inline-reply.png 1x,
60  {@docRoot}preview/images/inline-reply_2x.png 2x" width="400">
61<p class="img-caption">
62  <strong>圖 1.</strong> Android N 新增「回覆」<strong></strong>
63  操作按鈕.
64</p>
65
66<h3>新增內嵌回覆動作</h3>
67
68<p>建立支援直接回覆的通知動作:
69</p>
70
71<ol>
72<li>建立您可以新增至通知動作的 {@link android.support.v4.app.RemoteInput.Builder}
73  實例。
74此類別的建構函式接受字串,系統可當成文字輸入的金鑰使用。
75稍後,您的手持裝置應用程式會使用該金鑰,擷取輸入的文字。
76
77
78<pre>
79// Key for the string that's delivered in the action's intent
80private static final String KEY_TEXT_REPLY = "key_text_reply";
81String replyLabel = getResources().getString(R.string.reply_label);
82RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
83        .setLabel(replyLabel)
84        .build();
85</pre>
86</li>
87<li>使用 <code>addRemoteInput()</code> 將 {@link android.support.v4.app.RemoteInput}
88 物件附加到動作。
89
90<pre>
91// Create the reply action and add the remote input
92Notification.Action action =
93        new Notification.Action.Builder(R.drawable.ic_reply_icon,
94                getString(R.string.label), replyPendingIntent)
95                .addRemoteInput(remoteInput)
96                .build();
97</pre>
98</li>
99
100<li>將動作套用到通知並發出通知。
101
102<pre>
103// Build the notification and add the action
104Notification notification =
105        new Notification.Builder(mContext)
106                .setSmallIcon(R.drawable.ic_message)
107                .setContentTitle(getString(R.string.title))
108                .setContentText(getString(R.string.content))
109                .addAction(action))
110                .build();
111
112// Issue the notification
113NotificationManager notificationManager =
114        NotificationManager.from(mContext);
115notificationManager.notify(notificationId, notification);
116
117</pre>
118</li>
119
120</ol>
121
122
123<p> 當使用者觸發通知動作時,系統會提示使用者輸入回應。
124 </p>
125
126<img id="fig-user-input" src="{@docRoot}preview/images/inline-type-reply.png" srcset="{@docRoot}preview/images/inline-type-reply.png 1x,
127    {@docRoot}preview/images/inline-type-reply_2x.png 2x" width="300">
128<p class="img-caption">
129  <strong>圖 2.</strong> 使用者從通知欄輸入文字。
130</p>
131
132<h3>從內嵌回覆擷取使用者輸入</h3>
133
134<p>從您在回覆動作的意圖中所宣告動作的通知介面接收使用者輸入:
135</p>
136<ol>
137<li> 透過將通知動作的意圖傳遞為輸入參數,來呼叫 {@link android.support.v4.app.RemoteInput#getResultsFromIntent
138  getResultsFromIntent()}。
139這個方法會傳回包含文字回應的 {@link android.os.Bundle}。
140
141</li>
142
143<pre>
144Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
145</pre>
146
147<li>使用 (提供給 {@link
148  android.support.v4.app.RemoteInput.Builder} 建構函式的) 結果金鑰查詢組合。
149</li>
150</ol>
151
152<p>下列程式碼片段說明方法如何從組合中擷取輸入文字:
153</p>
154
155<pre>
156// Obtain the intent that started this activity by calling
157// Activity.getIntent() and pass it into this method to
158// get the associated string.
159
160private CharSequence getMessageText(Intent intent) {
161    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
162    if (remoteInput != null) {
163            return remoteInput.getCharSequence(KEY_TEXT_REPLY);
164            }
165    return null;
166 }
167</pre>
168
169<p>應用程式可以套用邏輯,決定擷取文字時要採取的動作。對於互動式應用程式 (像是聊天),要在通知本身提供更多內容 (例如,多行聊天歷程記錄,包括使用者自己的訊息),使用者才能適當回應。當使用者透過 {@link android.support.v4.app.RemoteInput} 回應時,請使用 {@code setRemoteInputHistory()}
170 方法在回覆歷程記錄中包括文字。
171
172
173
174
175
176</p>
177
178<h2 id="bundle">整合式通知</h2>
179
180<p>Android N 提供開發人員呈現通知佇列的全新方式:
181 <i>整合式通知</i>。這類似於 Android Wear
182  中的<a href="{@docRoot}training/wearables/notifications/stacks.html">通知堆疊</a>。
183例如,若您的應用程式會為接收的訊息建立通知,收到多個訊息時,會將通知結合成單一群組。
184
185您可以使用現有的 {@link android.support.v4.app.NotificationCompat.Builder#setGroup
186Builder.setGroup()} 方法,結合類似的通知。
187</p>
188
189<p>
190  通知群組會對其中所含的通知強制施行階層。
191  階層的最上方是上層通知,顯示該群組的摘要資訊。
192使用者可以逐漸擴充通知群組,而系統會在使用者更深入探查時顯示更多資訊。
193
194當使用者擴充組合時,系統會為所有子通知揭露更多資訊。當使用者擴充當中的其中一個通知時,系統會顯示該通知的所有內容。
195
196
197</p>
198
199<img id="fig-bundles" src="{@docRoot}preview/images/bundles.png" srcset="{@docRoot}preview/images/bundles.png 1x,
200          {@docRoot}preview/images/bundles_2x.png 2x" width="300">
201<p class="img-caption">
202  <strong>圖 3.</strong> 使用者可以逐漸擴充通知群組。
203
204</p>
205
206<p>若要了解如何將通知新增至群組,請參閱<a href="{@docRoot}training/wearables/notifications/stacks.html#AddGroup">將每個通知新增至群組</a>。
207
208</p>
209
210
211<h3 id="best-practices">整合式通知最佳做法</h3>
212<p>本節提供使用通知群組時的指導方針,而不是舊版 Android 平台可用的 {@link android.app.Notification.InboxStyle InboxStyle}
213通知。
214
215</p>
216
217<h3>使用整合式通知的時機</h3>
218
219<p>只有當您的使用案例與下列條件全部相符時,才應該使用通知群組:
220</p>
221
222<ul>
223  <li>子通知是完整通知,也能個別顯示,而不需要群組摘要。
224</li>
225  <li>讓子通知個別出現有其優點。例如:
226
227  </li>
228  <ul>
229    <li>能以特定動作來對每個子通知執行動作。</li>
230    <li>可讓使用者想讀取的子通知能有更多資訊。</li>
231  </ul>
232</ul>
233
234<p>良好的通知群組使用案例範例包括:顯示一連串傳入訊息的訊息傳送應用程式,或顯示一系列所接收電子郵件清單的電子郵件應用程式。
235
236</p>
237
238<p>
239建議使用的單一通知案例範例包括:來自單人的個別訊息,或以清單呈現單行的文字項目。
240
241您可以使用
242({@link android.app.Notification.InboxStyle InboxStyle} 或
243{@link android.app.Notification.BigTextStyle BigTextStyle}) 來完成。
244
245</p>
246
247<h3 id ="post">顯示整合式通知</h3>
248
249<p>
250  應用程式應一律張貼群組摘要,即使群組當中只包含單一子項。
251如果只包含單一通知,系統會抑制摘要,並直接顯示子通知。
252這可確保當使用者滑動離開群組子項時,系統能提供一致的體驗。
253
254
255</p>
256
257<p class="note">
258  <strong>注意:</strong>本版 Android N 還不會抑制包含單一子通知的通知群組摘要。
259更新的 Android N 版本才會新增此功能。
260
261</p>
262
263<h3>預覽通知</h3>
264
265<p>雖然系統通常會將子通知顯示為群組,但您可以設定子通知,暫時顯示為<a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#Heads-up">抬頭通知</a>
266267
268此功能允許立即存取最新的子通知和與它相關的動作,因此特別實用。
269
270</p>
271
272
273<h3>回溯相容性</h3>
274
275<p>
276  自從 Android 5.0 (API 層級 21) 支援 Android Wear 裝置以來,通知群組與遠端輸入都是 {@link
277  android.app.Notification} API 的一部分。
278如果您已使用這些 API 建置通知,您必須採取的動作只有確認應用程式行為符合上述的指導方針,以及考慮實作 {@code
279  setRemoteInputHistory()}。
280
281
282</p>
283
284<p>
285  為了支援回溯相容性,相同的 API 可與支援程式庫的 {@link android.support.v4.app.NotificationCompat}
286 類別搭配使用,讓您建置能在舊版 Android 上運作的通知。
287
288在手持裝置與平板電腦上,使用者只會看到摘要通知,應用程式應仍要為群組的完整資訊內容提供收件匣樣式或同等的通知呈現方式。
289
290雖然 Android
291  Wear 裝置即使在較舊的平台層級上,也允許使用者查看所有子通知,但不論 API 層級為何,您都應該建置子通知。
292
293
294</p>
295
296<h2 id="custom"> 自訂檢視</h2>
297<p>從 Android N 開始,您可以自訂通知檢視,也仍然可以取得系統裝飾,例如通知標題、動作及可擴充的版面配置。
298
299</p>
300
301<p>若要啟用此功能,Android N 新增下列 API 供您設定自訂檢視的樣式:
302</p>
303
304<dl>
305<dt>
306{@code DecoratedCustomViewStyle()}</dt>
307<dd> 設定媒體通知以外的通知樣式。
308</dd>
309<dt>
310{@code DecoratedMediaCustomViewStyle()}</dt>
311<dd> 設定媒體通知樣式</dd>
312</dl>
313
314<p>若要使用這個新 API,請呼叫 {@code setStyle()} 方法,再傳遞給想要的自訂檢視樣式。
315</p>
316
317<p>此程式碼片段顯示如何使用
318{@code DecoratedCustomViewStyle()} 方法,建構自訂通知物件。</p>
319
320<pre>
321Notification noti = new Notification.Builder()
322           .setSmallIcon(R.drawable.ic_stat_player)
323           .setLargeIcon(albumArtBitmap))
324           .setCustomContentView(contentView);
325           .setStyle(new Notification.DecoratedCustomViewStyle())
326           .build();
327
328</pre>
329