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

Android 5.x Notification 解析:普通、折疊與懸掛式通知

編輯:關於 Android 程式設計

很難想像沒有 Notification 的手機生活:QQ、微信、各種 App 都靠它主動提醒我們,否則每分鐘都得自己解鎖檢查有沒有新訊息。以下介紹三種通知形式,以及 Android 5.0 新增的顯示等級設定。

一、普通 Notification

先建立 Builder,用 PendingIntent 控制點擊後的跳轉:

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

接著設定各項屬性:

builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);          // 點擊後自動消失
builder.setContentTitle("普通通知");

最後透過 NotificationManager 發出:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

二、折疊式 Notification

折疊式通知的重點是「收起時一個樣子、展開後另一個樣子」,因此要用 RemoteViews 自訂視圖。

// 建立自訂通知視圖
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);

對應的版面檔:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/fold"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/fold" />

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="30dp"
        android:text="展開後的自訂視圖" />

</LinearLayout>

接著決定這個自訂視圖要套在哪個狀態:

// 指定展開時的視圖
notification.bigContentView = remoteViews;

// 若要指定普通(收起)狀態的視圖
notification.contentView = remoteViews;

完整程式碼:

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("折疊式通知");

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
Notification notification = builder.build();
notification.bigContentView = remoteViews;   // 展開時顯示

notificationManager.notify(1, notification);

如果沒有自訂收起狀態的視圖,折疊式通知在收起時看起來就跟普通通知沒兩樣:

往下拉到底展開後,才會看到自訂的版面:

三、懸掛式 Notification

懸掛式(Heads-up)是 Android 5.0 新增的顯示方式。與前兩者的差別在於:它不需要下拉通知欄,會直接浮在螢幕上方,而且不改變目前焦點,使用者原本的操作不會被打斷,幾秒後自動消失。

關鍵是改用 setFullScreenIntent():

// 若描述相符的 PendingIntent 已存在,會先取消再建立新的
hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);

完整程式碼:

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("懸掛式通知");

Intent hangIntent = new Intent();
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
hangIntent.setClass(this, MyNotificationActivity.class);
hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

builder.setFullScreenIntent(hangPendingIntent, true);
notificationManager.notify(2, builder.build());

四、通知的顯示等級

Android 5.0 加入了通知在鎖屏狀態下的可見性設定,共三種:

  • VISIBILITY_PUBLIC:任何情況都完整顯示。
  • VISIBILITY_PRIVATE:鎖屏時隱藏敏感內容。
  • VISIBILITY_SECRET:安全鎖定時完全不顯示通知。

設定方式很簡單:

builder.setVisibility(Notification.VISIBILITY_PUBLIC);

用 RadioGroup 做一個切換示範:

private void selectNotificationLevel(Notification.Builder builder) {
    switch (radioGroup.getCheckedRadioButtonId()) {
        case R.id.rb_public:
            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            builder.setContentText("public");
            break;
        case R.id.rb_private:
            builder.setVisibility(Notification.VISIBILITY_PRIVATE);
            builder.setContentText("private");
            break;
        case R.id.rb_secret:
            builder.setVisibility(Notification.VISIBILITY_SECRET);
            builder.setContentText("secret");
            break;
        default:
            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            builder.setContentText("public");
            break;
    }
}

版本演進提醒

本文以 Notification.Builder 為例,這是 5.x 時代的寫法。從 Android 8.0(API 26)開始,所有通知都必須綁定通知渠道(NotificationChannel),否則通知根本不會顯示;同時也必須動態申請 POST_NOTIFICATIONS 權限(API 33+)。目前建議改用 NotificationCompat.Builder 搭配 AndroidX,才能同時兼顧新版 API 與向下相容。

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