解决Android 8.0 的Notification不显示问题

本文详细介绍如何在Android O及以上版本中创建通知渠道,并针对不同版本进行适配,包括设置通知标题、内容、图标及PendingIntent跳转。同时,探讨了如何在老版本中使用NotificationCompat.Builder实现相似功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原作者地址:地址

String id = "channel_001";
String name = "name";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
    notificationManager.createNotificationChannel(mChannel);
    notification = new Notification.Builder(context)
            .setChannelId(id)
            .setContentTitle("测试活动1")
            .setContentText("您有一项新活动")
            .setSmallIcon(R.drawable.a).build();
} else {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setContentTitle("测试活动2")
            .setContentText("您有一项新活动")
            .setSmallIcon(R.drawable.b)
            .setOngoing(true)
            .setChannelId(id);//无效
    notification = notificationBuilder.build();
}
notificationManager.notify(1, notification);

增加点击跳转和点击删除操作

//设置要跳转的页面
PendingIntent intent = PendingIntent.getActivity(this,0,new Intent(this, Test.class),PendingIntent.FLAG_UPDATE_CURRENT);

在这里插入图片描述
在这里插入图片描述
文档地址

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值