Android O 适配 Notification Channel

code小生,一个 Android 领域技术分享平台

作者:许渺
链接:https://www.jianshu.com/p/b529e61d220a
声明:本文是 许渺 原创,转发等请联系原作者授权。

背景

从Android 8.0(API 26)开始,所有的 Notification 都要指定 Channel(通道),对于每一个 Channel 你都可以单独去设置它;比如通知开关、提示音、是否震动或者是重要程度等;这样每个应用程序的通知在用户面前都是透明的。

下面我们来看一下通知的设置页面和 Channel 的设置界面

Notification Setting

这边通知设置界面中的类别指的就是 Channel,你必须要创建一个或者多个Channel;这边需要注意的是如果你的 tartgetSdkVersion>=26,如果你发布通知不指定 Channel 的话,通知是不会显示的(系统会自动记录错误)。

Channel Setting

创建 Notification Channel

  1. 创建 NotificationChannel 对象,指定 Channel 的 id、name 和通知的重要程度。

  2. setDescription 可以指定设置中 Channel 的描述,如上图中的(this is default channel!)

  3. 使用NotificationMannager的createNotificationChannel方法来添加Channel。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
   mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
   mNotificationChannel.setDescription(CHANNEL_DESCRIPTION);
   getNotificationManager().createNotificationChannel(mNotificationChannel);
}

设置通知重要性级别

该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
,如果要支持 Android 7.1(API 25)及以下的设备,还得调用NotificationCompat 的 setPriority 方法来设置,如下所示

builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

我们总结一下;Android 8.0 及以上是使用NotificationManager.IMPORTANCE_,Android 7.1 及以下是使用NotificationCompat.PRIORITY_它们都是定义的常量;下面我们以表格的形式更好的展示出来。

对于上面这些通知级别用户都是可以在 Channel 设置中更改的,嗯就是这样!

打开 Channel 设置

为了让用户能够轻松访问 Channel 设置,我们可以通过下面的代码在 APP 中加入设置入口点,这样用户体验可能会更好!

public void openChannelSetting(String channelId)
{
   Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
   intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
   intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
   if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
       startActivity(intent);
}

打开通知设置

public void openNotificationSetting()
{
   Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
   intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
   if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
       startActivity(intent);
}

最后贴一下封装的代码(封装的不是很好)

/**
* @author xuyj
*/

public class NotificationHelper extends ContextWrapper
{
   private NotificationManager mNotificationManager;
   private NotificationChannel mNotificationChannel;

   public static final  String CHANNEL_ID          = "default";
   private static final String CHANNEL_NAME        = "Default Channel";
   private static final String CHANNEL_DESCRIPTION = "this is default channel!";

   public NotificationHelper(Context base)
   
{
       super(base);
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
       {
           mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
           mNotificationChannel.setDescription(CHANNEL_DESCRIPTION);
           getNotificationManager().createNotificationChannel(mNotificationChannel);
       }
   }

   public NotificationCompat.Builder getNotification(String title, String content)
   
{
       NotificationCompat.Builder builder = null;
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
       {
           builder = new NotificationCompat.Builder(this, CHANNEL_ID);
       } else
       {
           builder = new NotificationCompat.Builder(this);
           builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
       }
       builder.setContentTitle(title);
       builder.setContentText(content);
       builder.setSmallIcon(R.mipmap.comments);
       builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.comments));
       //点击自动删除通知
       builder.setAutoCancel(true);
       return builder;
   }

   public void notify(int id, NotificationCompat.Builder builder)
   
{
       if (getNotificationManager() != null)
       {
           getNotificationManager().notify(id, builder.build());
       }
   }

   public void openChannelSetting(String channelId)
   
{
       Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
       intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
       intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
       if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
           startActivity(intent);
   }

   public void openNotificationSetting()
   
{
       Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
       intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
       if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null)
           startActivity(intent);
   }

   private NotificationManager getNotificationManager()
   
{
       if (mNotificationManager == null)
           mNotificationManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
       return mNotificationManager;
   }

}

Demo地址

https://link.jianshu.com/?t=https%3A%2F%2Fgithub.com%2FXuMiaoLee%2FAndroidNotificationChannel.git

参考

https://developer.android.google.cn/training/notify-user/channels.html


https://github.com/googlesamples/android-NotificationChannels/#readme




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android常驻通知(Notification)是指在用户状态栏中一直显示的通知图标和文本内容,不会因为用户操作或应用进程被销毁而消失。常驻通知通常用于实时监测、后台服务、音乐播放等需要持续提醒用户的场景。 常驻通知的实现步骤如下: 1. 首先,需要创建一个Notification对象,包括通知图标、标题、内容等信息。 2. 然后,创建一个PendingIntent,用于定义用户点击通知后的操作,比如打开应用的某个Activity或执行某个Service。 3. 创建一个NotificationChannel(通知渠道),用于定义通知的重要程度,包括声音、震动等设置。 4. 将Notification对象与PendingIntent关联,并将其设置为常驻通知的优先级。 5. 最后,调用NotificationManager的notify方法,将通知显示在用户的状态栏上。 需要注意的是,常驻通知存在一些使用限制和最佳实践: 1. 用户可以通过设置中的通知管理来关闭或打开特定应用的常驻通知。 2. 常驻通知不适合用于广告或频繁推送的内容,以免打扰用户。 3. 为了避免误导用户,常驻通知的图标和文本内容应与应用的实际情况相符。 4. 如果需要更新通知的内容或操作,可以使用NotificationManager的notify方法进行更新,并保持通知的id不变。 总之,常驻通知Android提供的一个重要功能,可以实现持续提醒用户和后台监测的需求。但应用开发者需要注意使用场景和用户体验,遵循Android的最佳实践,以确保用户对常驻通知的接受和理解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值