android 8.0适配Notification

拿一个以前的项目来重构,发现notification没有效果了,也没有报错,有的手机上能显示通知,但是没有action。奇怪,查了一下才知道是要适配了,我将新项目中的targetSdkVersion指定到了26以上,就强制要适配了。

从Android 8.0系统开始,Google引入了通知渠道这个概念。就是说每条通知都有一个渠道属性。App都可以自由选择拥有哪些通知渠道,但是通知渠道的控制权都是掌握在用户手上,用户可以自由地选择这些通知渠道的重要程度,是否响铃、是否振动、或者是否要关闭这个渠道的通知。

背景就交代到这里,here we go

 

由上图可见NotificationCompat.Builder的构造方法Builder(Context context)已经被标注过时了,“当然,Google也并没有完全做绝,即使方法标为了废弃,但还是可以正常使用的。可是如果你将项目中的targetSdkVersion指定到了26或者更高,那么Android系统就会认为你的App已经做好了8.0系统的适配工作,当然包括了通知栏的适配。这个时候如果还不使用通知渠道的话,那么你的App的通知将完全无法弹出”

新的构造方法多了一个参数channelId,也就是前面说的通知渠道,要提前创建渠道

@TargetApi(26)
private void createNotificationChannels() {
    List<NotificationChannel> notificationChannels = new ArrayList<>();
    NotificationChannel recordingNotificationChannel = new NotificationChannel(
            NotificationUtil.channelId,
            NotificationUtil.channelName,
            NotificationManager.IMPORTANCE_DEFAULT
    );
    recordingNotificationChannel.enableLights(true);
    recordingNotificationChannel.setLightColor(Color.RED);
    recordingNotificationChannel.setShowBadge(true);
    recordingNotificationChannel.enableVibration(true);
    recordingNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    notificationChannels.add(recordingNotificationChannel);
    getManager().createNotificationChannels(notificationChannels);
}

然后在new Builder的时候传入channel Id

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NotificationUtil.channelId)

创建通知的代码和传统创建通知的方法没什么两样,只是在NotificationCompat.Builder中需要多传入一个通知渠道ID。

详细参考郭神的文章:https://blog.csdn.net/guolin_blog/article/details/79854070

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 8.0 及以上版本,为了增强应用程序的安全性,Android 引入了后台限制,禁止未在前台运行的应用程序启动服务。如果您想在后台启动服务,需要使用 `startForegroundService()` 方法。这个方法会启动一个前台服务,然后你可以在服务启动后在通知栏显示一个通知,以此来告知用户服务正在运行。 以下是一个使用 `startForegroundService()` 的示例代码: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建一个 NotificationChannel NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT); // 向系统注册 NotificationChannel NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } // 创建一个 Intent,启动你的服务 Intent serviceIntent = new Intent(this, YourService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 在 Android 8.0 及以上版本上,需要调用 startForegroundService() 方法启动服务。 startForegroundService(serviceIntent); } else { // 在 Android 8.0 以下版本上,可以直接调用 startService() 方法启动服务。 startService(serviceIntent); } ``` 注意:如果你使用的是 `startForeground()` 方法,会在 Android 8.0 及以上版本上抛出 `IllegalStateException` 异常,因为 Android 8.0 及以上版本禁止在后台启动服务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值