第十八章 Notification

建立Notification

1. 使用new Notification.Builder(Context)建立builder物件
2. 在由builder.build()產生Notification 物件
3. 取得NotificationManager manager
4. manager.notify(int, notification)發送通知

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Notification.Builder builder=new Notification.Builder(this);
        builder
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Thomas Notification")
                .setContentText("這是一個通告");
        Notification notification=builder.build();
        NotificationManager manager=
                (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, notification);
    }
}

設定Notification

上述的builder中, 是最基本的需求設定, 常有如下項目可設定
.setColor(Color) : API21 Lollipop才支援
.setDefaults(int) : int可以為

Noitifcation.DEFAULT_VIBRATE|
Notification.DEFAULT_SOUND|
Notification.DEFAULT_LIGHTS

若要產生震動, 請加入權限
<uses-permission android:name=”android:permission.VIBRATE” />

設定特殊效果

Notification可以自訂需要的LED顏色, 震動模式及鈴聲
builder.setLights(Color, 開啟時間, 關閉時間) : LED不是每支手機都可支援, 要測試
builder.setVibrate(long[]) : 將要開啟及關閉的時間長度放入陣列中
builder.setSound(Uri) : 將音樂放入Uri即可

public class MainActivity extends AppCompatActivity {
    NotificationManager manager;
    Notification notification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Thomas Notification")
                .setContentText("這是一個通告");
        builder.setLights(Color.RED, 1000, 1000);
        long[] vibrate_effect={1000,500,1000,400,1000,300,1000,200,1000,100};
        builder.setVibrate(vibrate_effect);
        Uri uri=Uri.parse("android.resource://"+getPackageName()+"/raw/sun");
        builder.setSound(uri);
        notification=builder.build();
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }
    public void btn1_click(View view){
        manager.notify(0, notification);
    }
    public void btn2_click(View view){
        manager.cancel(0);
    }
}

進度條

要顯示進度條, 只需在builder中使用 .setProgress(int, int, boolean). 第一個參數為進度的最大值. 第二個參數為目前的進度. 第三個參數若為true, 表示不知確切的進度, 若為false則表示知道明確的進度.
builder.setOngoing(true)表示不能自通知區中移除.

上述設定好後, 再使用相同的通知編號一直發送通知即可. 要重複發送通知, 可以使用AsyncTask類別, 置於背景中持續執行. 如下程式碼中, 使用 AsyncTask類別的execute()啟動即可

進度結束後, 再使用 setProgress(0,0, false)及 setOngoing(false), 然後再重送一次通知

android_notification_progress

public class MainActivity extends AppCompatActivity {
    NotificationManager manager;
    Notification.Builder builder;
    private final int code_id=13579;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        builder=new Notification.Builder(this);
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("下載更新程式")
                .setWhen(System.currentTimeMillis())
                .setOngoing(true);
        Progress progress=new Progress();
        progress.execute();
    }
    class Progress extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            for (int i=1;i<100;i++){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                builder.setProgress(100, i, false);
                builder.setContentText("下載進度 : "+i+"%");
                manager.notify(code_id, builder.build());
            }
            builder.setContentText("下載完成");
            builder.setProgress(0,0,false);
            builder.setOngoing(false);
            manager.notify(code_id, builder.build());
            return null;
        }
    }
}

大型通知畫面

新增Notification.BitPictureStyle pictureStyle, 再由pictureStyle的bigPicture(Bitmap)設定圖片及setSummaryText(String)設定說明. 最後使用builder的setStyle(pictureStyle)將style設定到builder即可.
注意圖片不可太大, 且小米手機無法使用, 而且不能使用NotivicationCompat.Builder

android_notification_picture

public class MainActivity extends AppCompatActivity {
    NotificationManager manager;
    Notification.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        builder=new Notification.Builder(this);
        builder
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Thomas Notification");
                //.setContentText("這是一個通告"); 在大圖片中此行無效
        Notification.BigPictureStyle pictureStyle=new Notification.BigPictureStyle();
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.thomas_ocp);
        pictureStyle.bigPicture(bitmap)
                .setSummaryText("大圖片");
        builder.setStyle(pictureStyle);
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }
    public void btn1_click(View view){
        manager.notify(0, builder.build());
    }
    public void btn2_click(View view){
        manager.cancel(0);
    }
}

若要顯示條列式通知文字, 可改為如下

android_notification_inbox

Notification.InboxStyle inboxStyle=new Notification.InboxStyle();
inboxStyle.addLine("這是第一行");
inboxStyle.addLine("這是第二行");
inboxStyle.addLine("這是第三行");
inboxStyle.setSummaryText("總共有三行");
builder.setStyle(inboxStyle);
manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *