日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Android給通知channel靜音的方法實例_Android

作者:BennuCTec ? 更新時間: 2022-04-16 編程語言

前言

目前各個市場都要求targetsdkversion要不低于26,也就是android 8.0。 相應(yīng)的影響很多功能,比如通知。

當(dāng)targetsdkversion >= 26,需要為通知添加channel,如

manager = (NotificationManager) BaseApp.getAppContext()
        .getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel("update", "update", NotificationManager.IMPORTANCE_DEFAULT);
    manager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(BaseApp.getAppContext(), "update");

首先要新建一個NotificationChannel,并調(diào)用NotificationManager的createNotificationChannel啟用該通道。

然后在創(chuàng)建buidler的時候設(shè)置同樣的channelid即可。

靜音

那么如果想讓通知靜音怎么處理?

  • 可以在channel上設(shè)置,setSound(null, null)。注意如果已經(jīng)安裝且這個channel已經(jīng)存在,再覆蓋安裝不能生效,需要卸載重裝。
  • 也可以對builder進(jìn)行設(shè)置,setOnlyAlertOnce(true)。注意這個并不是完全靜音,而是讓這個通知只響一次,比如顯示下載進(jìn)度時,就不會一直響。
manager = (NotificationManager) BaseApp.getAppContext()
        .getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel("update", "update", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setSound(null, null);
    manager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(BaseApp.getAppContext(), "update");
...
builder.setOnlyAlertOnce(true);

總結(jié)

原文鏈接:https://juejin.cn/post/7062616850005229582

欄目分類
最近更新