張萌&韓墨羽——Android Notification通知

目錄

在這裏插入圖片描述

一、通知的基本使用

(1)、效果圖

(2)、使用步驟

(3)、細節使用

二、通知的進階使用

(1)、設置聲音

(2)、設置振動

(3)、設置LED燈閃爍

(4)、進行默認效果設置

三、通知的高級使用

(1)、設置富文本信息

(2)、設置帶有圖片消息

(3)、設置通知重要程度

一、通知的基本使用
(1)、效果圖
在這裏插入圖片描述

(2)、使用步驟
1、首先需要一個NotificationManager來進行管理,可以調用Context的getSystemService方法獲取,這裏傳入一個Context。NOTIFICAATION_SERVICE即可。

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2、需要使用一個Builder構造器來創建Notification對象,由於API不同會造成不同版本的通知出現不穩定的問題, 所以這裏使用NotificationCompat類來兼容各個版本。

Notification notification = new NotificationCompat.Builder(MainActivity.this).build();

3、基本設置

.setContentTitle("這是測試通知標題")  //設置標題
.setContentText("這是測試通知內容") //設置內容
.setWhen(System.currentTimeMillis())  //設置時間
.setSmallIcon(R.mipmap.ic_launcher)  //設置小圖標
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))   //設置大圖標

4、調用notify()讓通知顯示出來(第一個參數是ID, 保證每個通知所指定的id都是不同的,第二個參數是notification對象)

manager.notify(1,notification);

(3)、細節使用
  <一>、跳轉功能:
使用PendingIntent進行通知點擊跳轉功能。
PendingIntent的用法:
(1)、通過getActivity()、getBroadcast()、getService()方法獲取實例
(2)、參數(Context context, int requestCode, Intent intent, int flags)
第一個參數:Context
第二個參數:requestCode 一般用不到 ,通常設置爲0
第三個參數:intent
第四個參數:flags 用於確定PendingIntent的行爲。這裏傳0就行
(3)、使用方法

 Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("這是測試通知標題")  //設置標題
                        .setContentText("這是測試通知內容") //設置內容
                        .setWhen(System.currentTimeMillis())  //設置時間
                        .setSmallIcon(R.mipmap.ic_launcher)  //設置小圖標  只能使用alpha圖層的圖片進行設置
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))   //設置大圖標
                        .setContentIntent(pi)
 //                       .setAutoCancel(true)
                        .build();
                manager.notify(1,notification);

<二>、通知取消:
我們發現當點擊查看通知後,通知欄中還保留着通知圖標,我們怎麼取消呢
(1)、使用setAutoCancel(true)

Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ...
                       .setAutoCancel(true) //設置爲自動取消
                        .build();
                manager.notify(1,notification);

(2)、在跳轉後的Activity中

 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);

這裏的cancle傳入的是一個1,就是我們創建Notification中指定的通知的ID
在這裏插入圖片描述
二、通知的進階使用
都是一行代碼進行設置, 這裏就不分開寫了

 Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ...
                        .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) //設置通知提示音
                        .setVibrate(new long[]{0,1000,1000,1000}) //設置振動, 需要添加權限  <uses-permission android:name="android.permission.VIBRATE"/>
                        .setLights(Color.GREEN,1000,1000)//設置前置LED燈進行閃爍, 第一個爲顏色值  第二個爲亮的時長  第三個爲暗的時長
                        .setDefaults(NotificationCompat.DEFAULT_ALL)  //使用默認效果, 會根據手機當前環境播放鈴聲, 是否振動
                        .build();
                manager.notify(1,notification);

三、通知的高級使用
(1)、設置富文本信息
當我們使用setContentText的時候, 內容爲很長的字符串, 顯示結果是這樣的:
在這裏插入圖片描述
內容顯示不全。如果產品就要顯示完全的內容文本我們怎麼辦。
可以使用setStyle()

 Notification notification = new NotificationCompat.Builder(MainActivity.this)
                      ...
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("這是一段很長的文字很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長"))                      
                        .build();
                manager.notify(1,notification);

我們在setStyle()方法中創建了NotificationCompat.BigTextStyle對象。這個對象就是用於封裝長文本信息的,調用它的bigText()方法將文字傳入就行。
在這裏插入圖片描述

(2)、設置帶有圖片消息
在這裏插入圖片描述
Notification notification = new NotificationCompat.Builder(MainActivity.this)

.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)))
.build();
manager.notify(1,notification);

通知內加入圖片(大圖)中的調用方法

(3)、設置通知重要程度

Notification notification = new NotificationCompat.Builder(MainActivity.this)
                      ...
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .build();
                manager.notify(1,notification);

setPriority方法接收一個整形參數用於設置這條通知的重要程度, 有五個值可以選擇
PRIORITY_DEFAULT:表示默認重要程度,和不設置效果一樣
PRIORITY_MIN:表示最低的重要程度。系統只會在用戶下拉狀態欄的時候纔會顯示
PRIORITY_LOW:表示較低的重要性,系統會將這類通知縮小,或者改變顯示的順序,將排在更重要的通知之後。
PRIORITY_HIGH:表示較高的重要程度,系統可能會將這類通知方法,或改變顯示順序,比較靠前
PRIORITY_MAX:最重要的程度, 會彈出一個單獨消息框,讓用戶做出相應。
在這裏插入圖片描述
設置爲MAX顯示情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章