天天看點

全面了解Android Notification

什麼是通知(Notification)

通知是一個可以在應用程式正常的使用者界面之外顯示給使用者的消息。

通知發出時,它首先出現在狀态欄的通知區域中,使用者打開通知抽屜可檢視通知詳情。通知區域和通知抽屜都是使用者可以随時檢視的系統控制區域。

作為安卓使用者界面的重要組成部分,通知有自己的設計指南。在Android 5.0(API level 21)中引入的 Material Design 的變化是特别重要的,更多資訊請閱讀 通知設計指南。

如何建立通知

随着Android系統不斷更新,Notification的建立方式也随之變化,主要變化如下:

Android 3.0之前

Android 3.0 (API level 11)之前,使用

new Notification()

方式建立通知:

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);
           

Android 3.0 (API level 11)及更高版本

Android 3.0開始棄用

new Notification()

方式,改用

Notification.Builder()

來建立通知:

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setContentIntent(contentIntent)
            .build();   // getNotification()  deprecated in API level 16

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);
           

這裡需要注意: “build()” 是Androdi 4.1(API level 16)加入的,用以替代”getNotification()”。API level 16開始棄用”getNotification()”

相容Android 3.0之前的版本

為了相容

API level 11

之前的版本,

v4 Support Library

中提供了

NotificationCompat.Builder()

這個替代方法。它與

Notification.Builder()

類似,二者沒有太大差別。

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ResultActivity.class), 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setContentIntent(contentIntent);

mNotifyMgr.notify(NOTIFICATIONS_ID, mBuilder.build());
           

注:除特别說明外,本文将根據 NotificationCompat.Builder() 展開解析,Notification.Builder()類似。

通知基本用法

通知的必要屬性

一個通知必須包含以下三項屬性:

  • 小圖示,對應 setSmallIcon()
  • 通知标題,對應 setContentTitle()
  • 詳細資訊,對應 setContentText()

其他屬性均為可選項,更多屬性方法請參考NotificationCompat.Builder。

盡管其他都是可選的,但一般都會為通知添加至少一個動作(Action),這個動作可以是跳轉到Activity、啟動一個Service或發送一個Broadcas等。 通過以下方式為通知添加動作:

  • 使用PendingIntent
  • 通過大視圖通知的 Action Button //僅支援Android 4.1 (API level 16)及更高版本,稍後會介紹

建立通知

1、執行個體化一個NotificationCompat.Builder對象

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
           

NotificationCompat.Builder自動設定的預設值:

  • priority: PRIORITY_DEFAULT
  • when: System.currentTimeMillis()
  • audio stream: STREAM_DEFAULT

2、定義并設定一個通知動作(Action)

Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
            this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
           

3、生成

Notification

對象

Notificatioin notification = mBuilder.build();
           

4、使用

NotificationManager

發送通知

// Sets an ID for the notification
int mNotificationId = 001;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, notification);
           

更新通知

更新通知很簡單,隻需再次發送相同ID的通知即可,如果之前的通知依然存在則會更新通知屬性,如果之前通知不存在則重新建立。

示例代碼:

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
        .setContentTitle("New Message")
        .setContentText("You've received new messages.")
        .setSmallIcon(R.drawable.ic_notify_status);

int numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
    mNotifyBuilder.setContentText("new content text")
            .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is updated.
    mNotifyMgr.notify(notifyID, mNotifyBuilder.build());
...
           

取消通知

取消通知有如下4種方式:

  • 點選通知欄的清除按鈕,會清除所有可清除的通知
  • 設定了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,點選該通知時會清除它
  • 通過 NotificationManager 調用 cancel() 方法清除指定ID的通知
  • 通過 NotificationManager 調用 cancelAll() 方法清除所有該應用之前發送的通知

通知類型

大視圖通知

通知有兩種視圖:普通視圖和大視圖。

普通視圖:

全面了解Android Notification

大視圖:

全面了解Android Notification

預設情況下為普通視圖,可通過

NotificationCompat.Builder.setStyle()

設定大視圖。

注: 大視圖(Big Views)由Android 4.1(API level 16)開始引入,且僅支援Android 4.1及更高版本。

建構大視圖通知

以上圖為例:

1、建構Action Button的PendingIntent

Intent dismissIntent = new Intent(this, PingService.class);
dismissIntent.setAction(CommonConstants.ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

Intent snoozeIntent = new Intent(this, PingService.class);
snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);
           

2、建構NotificatonCompat.Builder對象

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_notification)
        .setContentTitle(getString(R.string.notification))
        .setContentText(getString(R.string.ping))
        .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
        // 該方法在Android 4.1之前會被忽略
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(msg))
        //添加Action Button
        .addAction (R.drawable.ic_stat_dismiss,
                getString(R.string.dismiss), piDismiss)
        .addAction (R.drawable.ic_stat_snooze,
                getString(R.string.snooze), piSnooze);
           

3、其他步驟與普通視圖相同

進度條通知

  • 明确進度的進度條

    使用

    setProgress(max, progress, false)

    來更新進度。

    max: 最大進度值

    progress: 目前進度

    false: 是否是不明确的進度條

    模拟下載下傳過程,示例如下:

    int id = 1;
    ...
    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("Picture Download")
        .setContentText("Download in progress")
        .setSmallIcon(R.drawable.ic_notification);
    
    // Start a lengthy operation in a background thread
    new Thread(
        new Runnable() {
            @Override
            public void run() {
                int incr;
                for (incr = 0; incr <= 100; incr+=5) {
                    mBuilder.setProgress(100, incr, false);
                    mNotifyManager.notify(id, mBuilder.build());
                    try {
                        // Sleep for 5 seconds
                        Thread.sleep(5*1000);
                    } catch (InterruptedException e) {
                        Log.d(TAG, "sleep failure");
                    }
                }
                mBuilder.setContentText("Download complete")//下載下傳完成           
                        .setProgress(0,0,false);    //移除進度條
                mNotifyManager.notify(id, mBuilder.build());
            }
        }
    ).start();
               
    全面了解Android Notification
    上圖,分别為下載下傳過程中進度條通知 和 下載下傳完成移除進度條後的通知。
  • 不确定進度的進度條

    使用

    setProgress(0, 0, true)

    來表示進度不明确的進度條

    mBuilder.setProgress(0, 0, true); mNotifyManager.notify(id, mBuilder.build());

    全面了解Android Notification

浮動通知(Heads-up Notifications)

Android 5.0(API level 21)開始,當螢幕未上鎖且亮屏時,通知可以以小視窗形式顯示。使用者可以在不離開目前應用前提下操作該通知。

如圖:

全面了解Android Notification
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
        .setContentTitle("New Message")
        .setContentText("You've received new messages.")
        .setSmallIcon(R.drawable.ic_notify_status)
        .setFullScreenIntent(pendingIntent, false);
           

以下兩種情況會顯示浮動通知:

  • setFullScreenIntent(),如上述示例。
  • 通知擁有高優先級且使用了鈴聲和振動

鎖屏通知

Android 5.0(API level 21)開始,通知可以顯示在鎖屏上。使用者可以通過設定選擇是否允許敏感的通知内容顯示在安全的鎖屏上。

你的應用可以通過

setVisibility()

控制通知的顯示等級:

  • VISIBILITY_PRIVATE : 顯示基本資訊,如通知的圖示,但隐藏通知的全部内容
  • VISIBILITY_PUBLIC : 顯示通知的全部内容
  • VISIBILITY_SECRET : 不顯示任何内容,包括圖示

自定義通知

Android系統允許使用RemoteViews來自定義通知。

自定義普通視圖通知高度限制為64dp,大視圖通知高度限制為256dp。同時,建議自定義通知盡量簡單,以提高相容性。

自定義通知需要做如下操作:

1、建立自定義通知布局

2、使用RemoteViews定義通知元件,如圖示、文字等

3、調用

setContent()

将RemoteViews對象綁定到NotificationCompat.Builder

4、同正常發送通知流程

注意: 避免為通知設定背景,因為相容性原因,有些文字可能看不清。

定義通知文本樣式

通知的背景顔色在不同的裝置和版本中有所不同,Android2.3開始,系統定義了一套标準通知文本樣式,建議自定義通知使用标準樣式,這樣有助于通知文本可見。

通知文本樣式:

Android 5.0之前可用:
android:style/TextAppearance.StatusBar.EventContent.Title    // 通知标題樣式  
android:style/TextAppearance.StatusBar.EventContent             // 通知内容樣式  

Android 5.0及更高版本:  
android:style/TextAppearance.Material.Notification.Title         // 通知标題樣式  
android:style/TextAppearance.Material.Notification                  // 通知内容樣式  
           

更多通知的标準樣式和布局,可參考源碼

frameworks/base/core/res/res/layout

路徑下的通知模版如:

Android 5.0之前:  
notification_template_base.xml  
notification_template_big_base.xml  
notification_template_big_picture.xml  
notification_template_big_text.xml  

Android 5.0 及更高版本:  
notification_template_material_base.xml  
notification_template_material_big_base.xml  
notification_template_material_big_picture.xml  
notification_template_part_chronometer.xml  
notification_template_progressbar.xml  

等等。
           

保留Activity傳回棧

正常Activity

預設情況下,從通知啟動一個Activity,按傳回鍵會回到主螢幕。但某些時候有按傳回鍵仍然留在目前應用的需求,這就要用到

TaskStackBuilder

了。

1、在manifest中定義Activity的關系

Android 4.0.3 及更早版本
<activity
    android:name=".ResultActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>

Android 4.1 及更高版本
<activity
    android:name=".ResultActivity"
    android:parentActivityName=".MainActivity">
</activity>
           

2、建立傳回棧PendingIntent

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// 添加傳回棧
stackBuilder.addParentStack(ResultActivity.class);
// 添加Intent到棧頂
stackBuilder.addNextIntent(resultIntent);
// 建立包含傳回棧的pendingIntent
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
           

上述操作後,從通知啟動ResultActivity,按傳回鍵會回到MainActivity,而不是主螢幕。

特殊Activity

預設情況下,從通知啟動的Activity會在近期任務清單裡出現。如果不需要在近期任務裡顯示,則需要做以下操作:

1、在manifest中定義Activity

<activity
    android:name=".ResultActivity"
    android:launchMode="singleTask"
    android:taskAffinity=""
    android:excludeFromRecents="true">
</activity>
           

2、建構PendingIntent

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notifyIntent = new Intent(this, ResultActivity.class);

// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent notifyPendingIntent =
        PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(notifyPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
           

上述操作後,從通知啟動ResultActivity,此Activity不會出現在近期任務清單中。

通知常見屬性和常量

通知的提醒方式

1、聲音提醒

  • 預設聲音

    notification.defaults |= Notification.DEFAULT_SOUND;

  • 自定義聲音

    notification.sound = Uri.parse(“file:///sdcard0/notification.ogg”);

2、震動提醒

  • 預設振動

    notification.defaults |= Notification.DEFAULT_VIBRATE;

  • 自定義振動

    long[] vibrate = {100, 200, 300, 400}; //震動效果

    // 表示在100、200、300、400這些時間點交替啟動和關閉震動 notification.vibrate = vibrate;

3、閃爍提醒

  • 預設閃爍

    notification.defaults |= Notification.DEFAULT_LIGHTS;

  • 自定義閃爍

    notification.ledARGB = 0xff00ff00; // LED燈的顔色,綠燈

    notification.ledOnMS = 300; // LED燈顯示的毫秒數,300毫秒

    notification.ledOffMS = 1000; // LED燈關閉的毫秒數,1000毫秒

    notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必須加上這個标志

常見的Flags

  • FLAG_AUTO_CANCEL

    當通知被使用者點選之後會自動被清除(cancel)

  • FLAG_INSISTENT

    在使用者響應之前會一直重複提醒音

  • FLAG_ONGOING_EVENT

    表示正在運作的事件

  • FLAG_NO_CLEAR

    通知欄點選“清除”按鈕時,該通知将不會被清除

  • FLAG_FOREGROUND_SERVICE

    表示目前服務是前台服務

更多Notification屬性詳見Notification。

That’s all! 更多通知知識點等待你來發掘,歡迎補充!

原文位址:http://connorlin.github.io/2016/04/21/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BA%86%E8%A7%A3Android-Notification%E5%90%97/

繼續閱讀