Android Notification一些坑

在Android Oreo上,使用PendingIntent.getBroadcast方式创建的通知可能出现不显示的问题,部分如vivo手机需要手动开启应用通知权限。同时,通知栏点击无响应,需通过设置明确的Intent使用setClass方法来解决。
摘要由CSDN通过智能技术生成

Android0上运行以前正常的通知可能会有各种问题

  • 1.通知栏不显示(使用PendingIntent.getBroadcast方式)

第一步添加代码如下

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channel = AppUtil.getChannel();
            NotificationChannel mChannel = new NotificationChannel(context.getPackageName(), channel, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(mChannel);
        }
        xxxxx
  builder.setChannelId(context.getPackageName());

第二:部分机型(比如vivio)需要在设置里面打开对应应用的通知开关

  • 2.通知栏点击没反应
    需要添加代码如下:
 Intent broadcastIntent = new Intent(actionNAme);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            broadcastIntent.setClass(context, A.Class);
        }

官方说明
I ran into the same, in Android Oreo you need to make it a explicit Intent (is not enough with putting the receiver on the manifest, in fact, it won’t pay attention to that), so when you make the intent, make it explicit using the setClass method:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值