1. 程式人生 > >【達內課程】Android中的Notification

【達內課程】Android中的Notification

什麼是通知

通知是Android中Service與使用者互動的一種方式(主要是Service)

在這裡插入圖片描述

一個傳送通知的栗子:

private static final int NOTIFICATION_ID = 1001;
private void sendNotification() {
        //1、NotificationManager
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        /** 2、Builder->Notification
         *  必要屬性有三項
         *  小圖示,通過 setSmallIcon() 方法設定
         *  標題,通過 setContentTitle() 方法設定
         *  內容,通過 setContentText() 方法設定*/
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentInfo("Content info")
                .setContentText("Content text")//設定通知內容
                .setContentTitle("Content title")//設定通知標題
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher_round)//不能缺少的一個屬性
                .setSubText("Subtext")
                .setTicker("滾動訊息......")
                .setWhen(System.currentTimeMillis());//設定通知時間,預設為系統發出通知的時間,通常不用設定
        Notification n = builder.build();
        //3、manager.notify()
        manager.notify(NOTIFICATION_ID,n);
}

執行在API22的手機上效果:
在這裡插入圖片描述

Android 8.0不能彈出通知

執行在API26的手機上怎麼樣呢…原來根本就不能彈出通知,填坑請戳下文:
Notification Android8.0中無法傳送通知,提示:No Channel found for pkg

一個相容8.0的栗子

在這裡插入圖片描述

private static final int NOTIFICATION_ID = 1001;
private void sendNotification() {
        //1、NotificationManager
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        /** 2、Builder->Notification
         *  必要屬性有三項
         *  小圖示,通過 setSmallIcon() 方法設定
         *  標題,通過 setContentTitle() 方法設定
         *  內容,通過 setContentText() 方法設定*/
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentInfo("Content info")
                .setContentText("Content text")//設定通知內容
                .setContentTitle("Content title")//設定通知標題
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher_round)//不能缺少的一個屬性
                .setSubText("Subtext")
                .setTicker("滾動訊息......")
                .setWhen(System.currentTimeMillis());//設定通知時間,預設為系統發出通知的時間,通常不用設定

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true); //是否在桌面icon右上角展示小紅點
            channel.setLightColor(Color.GREEN); //小紅點顏色
            channel.setShowBadge(true); //是否在久按桌面圖示時顯示此渠道的通知
            manager.createNotificationChannel(channel);
            builder.setChannelId("001");
        }

        Notification n = builder.build();
        //3、manager.notify()
        manager.notify(NOTIFICATION_ID,n);
}

讓通知常駐通知欄

在這裡插入圖片描述

//讓通知常駐通知欄
builder.setOngoing(true);

或者

Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;

如何清除通知

 private void clearNotification() {
        //單利的系統服務
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(NOTIFICATION_ID);
    }

如何更新通知介面內容

在這裡插入圖片描述

    private static final int NOTIFICATION_ID2 = 1002;
	private int progress = 0;
    private void sendNotification2() {
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("音樂下載")
                .setContentText("下載進度:"+progress+"%")
                .setSmallIcon(R.mipmap.ic_launcher)                ;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("002","download_channel",NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
            builder.setChannelId("002");
        }
        Notification n = builder.build();
        manager.notify(NOTIFICATION_ID2,n);
        progress+=10;
    }

點選通知執行意圖

在這裡插入圖片描述

sendNotification2方法修改如下:

Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//給通知新增點選意圖
builder.setContentIntent(pi);

在這裡插入圖片描述

如何實現滾動條的通知

在這裡插入圖片描述

private void sendProgressNotification() {
        final NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("進度")
                .setContentText("進度...")
                .setProgress(100,10,true);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("003","download_channel",NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
            builder.setChannelId("003");
        }

        Notification n = builder.build();
        manager.notify(NOTIFICATION_ID3,n);
        //每隔1秒更新進度條進度
        //啟動工作執行緒
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for(int i=1;i<=10;i++){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //發通知
                    builder.setProgress(100,i*10,false);
                    Notification n = builder.build();
                    manager.notify(NOTIFICATION_ID3,n);
                }
                super.run();
            }
        }.start();
    }

在這裡插入圖片描述