Android Oreo通知显示在状态栏中,但不显示在点通知中。
创始人
2024-08-14 22:00:54
0

要实现Android Oreo通知在状态栏中显示,但不在锁屏和点通知中显示,可以使用以下代码示例:

// 创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String channelId = "channel_id";
    CharSequence channelName = "Channel Name";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.enableVibration(true);
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);

    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(notificationChannel);
}

// 构建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("标题")
    .setContentText("内容")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setVisibility(NotificationCompat.VISIBILITY_SECRET)
    .setAutoCancel(true);

// 发送通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());

上述代码首先创建了一个通知渠道,并设置了锁屏可见性为VISIBILITY_SECRET,然后使用NotificationCompat.Builder构建通知,并将其锁屏可见性设置为VISIBILITY_SECRET。最后,使用NotificationManagerCompat发送通知。

这样设置后,通知将在状态栏中显示,但在锁屏和点通知中将不会显示。请注意,上述代码中的channel_id需要与通知渠道的ID相匹配。

相关内容

热门资讯

第1分钟教程!微乐小程序黑科技... 第1分钟教程!微乐小程序黑科技,微乐自建房免费黑科技下载苹果(开挂)切实是真的挂(2026)1)微乐...
经调查!微乐小程序黑科技,微乐... 经调查!微乐小程序黑科技,微乐小程序免费黑科技(透视)一贯真的是有挂(哔哩哔哩)1)微乐小程序免费黑...
4分钟脚本!微乐小程序免费黑科... 4分钟脚本!微乐小程序免费黑科技,微信小程序怎么开挂(开挂)本来真的是有挂(2026)微乐小程序免费...
网友热议!微乐小程序黑科技,微... 网友热议!微乐小程序黑科技,微乐自建房辅助软件(透视)切实真的是有挂(哔哩哔哩)1、游戏颠覆性的策略...
第一分钟教程!微乐小程序黑科技... 第一分钟教程!微乐小程序黑科技,微乐自建房辅助多少钱一个月(开挂)竟然有挂(2026)1、第一分钟教...
透视讲解!微乐小程序免费黑科技... 透视讲解!微乐小程序免费黑科技,微乐家乡麻辣自建房辅助app(透视)本来存在有挂(哔哩哔哩)1、超多...
四分钟教程!微乐小程序免费黑科... 四分钟教程!微乐小程序免费黑科技,微信微乐辅助器使用教程(开挂)总是真的是有挂(2026)1、微乐小...
透视黑科技!微乐小程序免费黑科... 透视黑科技!微乐小程序免费黑科技,微乐家乡官方免费下载安装(透视)竟然真的是有挂(哔哩哔哩)透视黑科...
第三分钟安装!微乐小程序黑科技... 第三分钟安装!微乐小程序黑科技,微乐辅助工具哪个好用(开挂)好像是有挂(2026)亲,关键说明,微乐...
透视有挂!微乐小程序黑科技,微... 透视有挂!微乐小程序黑科技,微信小程序微乐破解器2024(透视)切实是有挂(哔哩哔哩)暗藏猫腻,小编...