2014年9月19日 星期五

[Android] Notifications 訊息通知/狀態列顯示通知


因為專題需要用到這個功能,所以上網查找了下使用方法
意外發現有三種:
Notification
Notification.Builder
NotificationCompat.Builder

若依照現今API舊版到新版寫法就是:
Notification (API 11以下適用)
NotificationCompat.Builder (相容性寫法/折衷)
Notification.Builder (API 16 以上適用)

這篇文章將以NotificationCompat.Builder為準--


Notification.Builder | Android Developers


在經歷了舊版Notification後,發現在程式頁中使用到這方法時會被劃刪除線






研究了一下折衷版寫法,感覺更加方便和簡潔。

myNotification = new NotificationCompat.Builder(context)
          .setContentTitle("Title")
          .setContentText("Text")
          .setTicker("Notification")
          .setWhen(System.currentTimeMillis())
          .setContentIntent(pendingIntent)
          .setDefaults(Notification.DEFAULT_SOUND)
          .setAutoCancel(true)
          .setSmallIcon(R.drawable.ic_launcher)  //或.setLargeIcon(smallicon) 
          .build();

ContentTitle 設定上方狀態列拉下來後的詳細標題
Text則是詳細內容
Ticker就是那一行細細長長的狀態列
ContentIntent是用來設定點選該Notification後會跳轉的畫面,但實質上和Intent有些不同

SmallIcon 若無設定Large Icon,此為左邊的大Icon;設定了Large Icon的話則是右下角的小Icon,不管如何都會影響到Notifications area顯示的圖樣

說他方便的原因是,其餘還能在開發者團隊那找到其他功能,像是閃光、設定訊息數量等等指令,而且只需用.set來增加,何樂而不為。

以下是一些常用功能,特別提出來:

.setNumber()  控制通知數量

.setOngoing(true) 控制通知能否被移除,不設定或是參數換成false即可恢復一般
.setAutoCancel(true) 按下後會直接消失

.setVibrate(vT) 設定震動:
Ex: .setVibrate(new long[] {0,200,800,500})

效果為:延遲0毫秒→震動200毫秒→延遲800毫秒→震動500毫秒
也可另外宣告 Builder.build().vibrate = new long[] {0,200,800,500}; 

.setLights(argb, onMs, offMs) 設定三色燈的參數,閃光顏色/持續毫秒/停頓毫秒
Ex:.setLights(0xff0000ff,300,300)
argb通常會設定白光,可以根據flags處來調整,在此不贅述,請自行參閱開發者網站

[Notice]使用震動和閃光的時候要記得給權限


uses-permission android:name="android.permission.FLASHLIGHT"
uses-permission android:name="android.permission.VIBRATE"

如果不想特別設定,在setDefaults的地方可以用以下方法控制:
Notification.DEFAULT_VIBRATE   增加預設震動提示,需要Vibrate權限
Notification.DEFAULT_SOUND    增加預設聲音提示(收簡訊的音效)
Notification.DEFAULT_LIGHTS     增加三色燈的提示
Notification.DEFAULT_ALL          增加以上的提示功能


Example:
宣告:
private NotificationManager gNotMgr = null;


OnCreate部份:

gNotMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
RegistControls(this);
gNotMgr.cancelAll();

其他:
private void RegistControls(final Context context){
    Button tBtnN1 = (Button) findViewById(R.id.btnNotification1);
    tBtnN1.setOnClickListener(new View.OnClickListener() {   
    @Override
    public void onClick(View v) {
        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notification tBNot = new NotificationCompat.Builder(context)
            .setContentTitle("Title")
            .setContentText("Text")
            .setTicker("Notification")
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_launcher)
            .build();
        gNotMgr.notify(1, tBNot);    
        }
    });
}



沒有留言:

張貼留言