Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Notification 與 NotificationManager 完整指南

編輯:關於 Android 程式設計

通知是應用程式在「不跳出前景」的前提下與使用者溝通的手段,由系統的通知管理器統一管理。它能做的事包括:在狀態列顯示圖示、在展開的通知面板顯示詳細資訊(可附 Intent)、閃爍 LED、啟動震動、播放鈴聲或媒體庫音檔。

一、取得 NotificationManager

NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

透過它可以發送新通知、修改既有通知、或移除不再需要的通知。

二、建立基本通知

Notification notification = new Notification(
        R.drawable.icon,
        "在 EoeAndroidReceiver1 中",
        System.currentTimeMillis());

建構子需要三個參數:狀態列圖示、狀態列的提示文字(ticker text)、以及通知產生的時間。另外可設定 number 屬性,讓同一個圖示代表多個事件時顯示數量。

三、設定展開後的內容

有兩種方式配置通知面板的外觀:

  1. 用 setLatestEventInfo() 填入標準版面所需的欄位——標題與詳細說明字串。標準版面會自動使用建構子中指定的圖示與時間。
  2. 用 RemoteViews 設定 contentView 與 contentIntent,自訂整個面板 UI。
notification.setLatestEventInfo(context, "在 EoeAndroidReceiver1 中", null, contentIntent);

通知的目的通常是引導使用者採取行動,所以要指定一個 PendingIntent,在點擊時觸發:

PendingIntent contentIntent = PendingIntent.getActivity(
        context, 0, new Intent(context, ActivityMain.class), 0);
notification.setLatestEventInfo(context, "標題", "內容", contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

多數情況下這個 Intent 應該開啟 App 並導航到與通知內容相關的頁面。

四、提示方式:聲音、震動、燈光

使用系統預設值是最一致、最省事的做法,可任意組合:

notification.defaults = Notification.DEFAULT_LIGHTS
        | Notification.DEFAULT_SOUND
        | Notification.DEFAULT_VIBRATE;
// 或全部採用
notification.defaults = Notification.DEFAULT_ALL;

自訂鈴聲:把音檔的 URI 指派給 sound 屬性。自訂音檔需事先放到裝置上,或放進 res/raw:

Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = uri;
// 或使用內建媒體
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

震動:指派一個 long 陣列給 vibrate 屬性,元素依序代表「震動時間」與「暫停時間」交替:

long[] vibrate = {1000, 1000, 1000, 1000, 1000};
notification.vibrate = vibrate;

使用震動必須在 manifest 中宣告權限:

<uses-permission android:name="android.permission.VIBRATE"/>

LED:可控制顏色與閃爍頻率。各裝置對 LED 的控制能力不同,若指定顏色不可用,系統會挑最接近的顏色。

notification.ledARGB = Color.RED;
notification.ledOnMS  = 300;   // 亮
notification.ledOffMS = 1000;  // 滅
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

設定 LED 之後務必加上 FLAG_SHOW_LIGHTS,否則不會生效。若把 ledOnMS 設為 1、ledOffMS 設為 0 可讓 LED 長亮;兩者皆設 0 則關閉。

五、Flags 常用值

旗標效果
FLAG_AUTO_CANCEL點擊通知後自動清除
FLAG_INSISTENT重複發出聲音,直到使用者回應(通常用於鬧鐘、來電)
FLAG_ONGOING_EVENT放入通知面板的「進行中」分組,如下載進度
FLAG_NO_CLEAR點擊「清除通知」時不被移除,常與 FLAG_ONGOING_EVENT 搭配
FLAG_SHOW_LIGHTS啟用 LED 設定

另外 number 屬性代表目前事件數量,會疊在狀態列圖示上方;若要使用,數值須從 1 開始。

六、完整流程整理

// 1. 取得服務
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(ns);

// 2. 建立 Notification
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

// 3. 設定提示效果
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// 4. 設定點擊行為
Context context = getApplicationContext();
CharSequence contentTitle = "My Notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent =
        PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

// 5. 發送
private static final int ID_NOTIFICATION = 1;
mNotificationManager.notify(ID_NOTIFICATION, notification);

七、更新與移除

更新:只要重新設定 notification 並用同一個 ID 再呼叫一次 notify(),就會覆蓋原通知——這也是實作下載進度的標準做法。

移除:呼叫 mNotificationManager.cancel(ID_NOTIFICATION) 取消單一通知,或用 cancelAll() 一次清除全部。

八、自訂通知版面

若標準版面無法滿足需求,可透過 RemoteViews 指定自訂 layout。官方文件(SDK 內 docs/guide/topics/ui/notifiers/notifications.html)對此有詳細說明。

九、版本演進提醒

本文 API 屬於早期 Android 的通知模型。從 Android 5.0 起改為 Builder 模式,8.0(API 26)更強制要求所有通知都必須指定 NotificationChannel,否則完全不會顯示;setLatestEventInfo() 也早已移除。若你在維護舊專案,上述寫法仍然有效;若為新專案,請直接採用 NotificationCompat.Builder 搭配 NotificationManagerCompat,並記得建立 channel 與處理通知權限請求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved