? ?? ?? Android在8.0引入了一個(gè)叫NotificationChannel(通知渠道)的概念,使得同一個(gè)渠道的通知可以進(jìn)行統(tǒng)一管理晌畅,用戶(hù)可以通過(guò)屏蔽某個(gè)通知渠道來(lái)達(dá)到屏蔽該渠道下所有的通知。同一個(gè)通知渠道的通知都被折疊在通知下拉框中术徊,極大節(jié)省了的空間前塔。之前項(xiàng)目中遇到需要發(fā)簡(jiǎn)單通知的需求,在此做一個(gè)簡(jiǎn)單的8.0版本的通知適配式廷。工具類(lèi)使用單例來(lái)編寫(xiě)咐扭,在最后會(huì)給出所有代碼,可以根據(jù)自身需求進(jìn)行完善滑废。
NotificationChannel channel = new NotificationChannel("channel_id"," channel_name", NotificationManager.IMPORTANCE_DEFAULT);
傳入的參數(shù)前兩個(gè)顯而易見(jiàn)蝗肪,第三個(gè)是通知的優(yōu)先級(jí),8.0之前的通知優(yōu)先級(jí)的設(shè)置是在Notification.Builder中的setPriority()方法來(lái)設(shè)置策严。
NotificationChannel還有很多其他API可以來(lái)調(diào)用穗慕,如下代碼:
channel.setDescription("通道描述");
//可否繞過(guò)系統(tǒng)的 請(qǐng)勿打擾模式
channel.canBypassDnd();
channel.setBypassDnd(true);
//鎖屏是否顯示通知(系統(tǒng)應(yīng)用可用,基本用不到)
channel.setLockscreenVisibility(VISIBILITY_SECRET);
//通知時(shí)是否有閃光燈(需要手機(jī)硬件支持)
channel.enableLights(true);
channel.shouldShowLights();
//設(shè)置閃光時(shí)的顏色
channel.setLightColor(Color.BLUE);
//是否顯示桌面圖標(biāo)角標(biāo)
channel.canShowBadge();
channel.setShowBadge(true);
//是否允許震動(dòng)
channel.enableVibration(true);
//獲取系統(tǒng)通知響鈴的配置參數(shù)(一般用系統(tǒng)默認(rèn)的即可)
channel.getAudioAttributes();
//channel.setSound(); 設(shè)置通知鈴聲
//設(shè)置震動(dòng)頻率(100ms妻导,100ms逛绵,100ms 三次震動(dòng))
channel.setVibrationPattern(new long[]{500, 500, 500});
最重要的一步就是給通知設(shè)置渠道怀各,這一步必不可少,不然8.0以上機(jī)型不生效
//給8.0以上的通知設(shè)定 渠道
getNotificationManager().createNotificationChannel(channel);
Notification.Build沒(méi)有什么好敘述的了术浪,直接貼代碼
private void setBuilder() {
builder = new Notification.Builder(this)
.setSmallIcon(smallIcon)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(channelId);
}
}
大體上8.0的適配就是這樣瓢对,給通知增加一個(gè)通知渠道即可。如下貼出所有代碼
public class NotificationUtils extends ContextWrapper {
private String title;
private String content;
private int smallIcon;
private String channelName;
private String channelId;
private Notification.Builder builder;
private NotificationManager notificationManager;
private NotificationManager getNotificationManager() {
if (notificationManager == null) {
return notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
} else {
return notificationManager;
}
}
/**
* @param context
* @param channelName 通道名
* @param channelId 通道ID
* @param title 通知標(biāo)題
* @param content 通知內(nèi)容
* @param smallIcon 通知欄顯示的圖標(biāo)
*/
public NotificationUtils(Context context, String channelName, String channelId, String title, String content, int smallIcon) {
super(context);
this.channelName = channelName;
this.channelId = channelId;
this.title = title;
this.content = content;
this.smallIcon = smallIcon;
setBuilder();
setChannel();
}
/**
* 構(gòu)造Notification.Builder
*/
private void setBuilder() {
builder = new Notification.Builder(this)
.setSmallIcon(smallIcon)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(channelId);
}
}
/**
* 8.0 以上給通知設(shè)置通道
*/
private void setChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("通道描述");
//可否繞過(guò)系統(tǒng)的 請(qǐng)勿打擾模式
channel.canBypassDnd();
channel.setBypassDnd(true);
//鎖屏是否顯示通知(系統(tǒng)應(yīng)用可用胰苏,基本用不到)
channel.setLockscreenVisibility(VISIBILITY_SECRET);
//通知時(shí)是否有閃光燈(需要手機(jī)硬件支持)
channel.enableLights(true);
channel.shouldShowLights();
//設(shè)置閃光時(shí)的顏色
channel.setLightColor(Color.BLUE);
//是否顯示桌面圖標(biāo)角標(biāo)
channel.canShowBadge();
channel.setShowBadge(true);
//是否允許震動(dòng)
channel.enableVibration(true);
//獲取系統(tǒng)通知響鈴的配置參數(shù)(一般用系統(tǒng)默認(rèn)的即可)
channel.getAudioAttributes();
//channel.setSound(); 設(shè)置通知鈴聲
//設(shè)置震動(dòng)頻率(100ms硕蛹,100ms,100ms 三次震動(dòng))
channel.setVibrationPattern(new long[]{500, 500, 500});
//給8.0以上的通知設(shè)定 渠道
getNotificationManager().createNotificationChannel(channel);
}
}
/**
* 顯示通知
*
* @param notifyId
*/
public void notify(int notifyId) {
getNotificationManager().notify(notifyId, builder.build());
}
}
使用如下
public class MainActivity extends AppCompatActivity {
private NotificationUtils notificationUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationUtils = new NotificationUtils
(this, "渠道名稱(chēng)", "渠道ID", "標(biāo)題", "內(nèi)容", R.mipmap.ic_launcher);
notificationUtils.notify(1);
}
}
如果你需要意圖硕并,可以重寫(xiě)notify方法法焰,把PendingIntent傳入給build設(shè)置一下即可。