3 种 Notification

本文详细介绍了Android O (8.0)的通知机制变化,包括如何创建通知渠道、使用NotificationChannel进行通知管理,并展示了普通通知、折叠式通知及悬挂式通知的实现方式。

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

版本适配

Android O 8.0 通知新特性

参考:Android O(8.0) Notification解决方案

android 8.0通知新增了NotificationChannel(通知渠道),用来帮助管理用户通知。Android Studio新版会默认使用新版本的SDK编译项目,如果App的targetSDKVersion >=26或者Android Studio默认使用高于26的sdk作为targetSDK时,没有使用NotificationChannel就会出现在Android 8.0及以上系统通知无法展示的情况。

Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。比如聊天软件,为每个聊天组设置一个通知渠道,指定特定声音、灯光等配置。

PS:在创建 Notification.Builder 的时候使用双参数构造方法 public Builder(Context context, String channelId),单参数的构造方法已经被弃用,可能会因为没有配置 ChannelId 导致通知发送失败,即网上有的开发者说的Android 8.0通知不显示的问题。

普通 Notification

  1. 创建 Builder 对象,用 PendingIntent 控制 跳转,这里跳转到网页
 public static String CALENDAR_ID = "channel_01";
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(this, CALENDAR_ID);
            NotificationChannel channel = new NotificationChannel(CALENDAR_ID, "渠道名", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        } else {
            builder = new Notification.Builder(this);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  1. 给 Notification 添加各种属性
        builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentTitle("普通通知");
  1. 显示通知
        Notification notification = builder.build();
        notificationManager.notify(1, notification);
  1. 运行效果

在这里插入图片描述

折叠式 Notification

目前只测试了 Android N 7.0 (API 24)以上

  1. 展开的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="展开的视图"
        android:textColor="#000000" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:src="@mipmap/ic_launcher_round" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
</LinearLayout>
  1. 创建 Builder 对象,用 PendingIntent 控制 跳转,这里跳转到网页
 public static String CALENDAR_ID = "channel_01";
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification;
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android 8.0 及以上
            builder = new Notification.Builder(this, CALENDAR_ID);
            NotificationChannel channel = new NotificationChannel(CALENDAR_ID, "渠道名", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        } else {
            builder = new Notification.Builder(this);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  1. 给 Notification 添加各种属性
        builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentTitle("折叠式通知");

  1. 设置自定义的视图后并显示通知,如果不自定义折叠时的视图,普通状态下和普通 Notification 没有区别。
        notification = builder.build();
        // 折叠时的视图
        RemoteViews foldView = new RemoteViews(getPackageName(), R.layout.layout_fold);
        // 展开时的视图
        RemoteViews expandView = new RemoteViews(getPackageName(), R.layout.layout_expand);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.setCustomContentView(foldView);
            builder.setCustomBigContentView(expandView);
        } else {
            notification.contentView = foldView;
            notification.bigContentView = expandView;
        }
        notificationManager.notify(2, notification);
  1. 运行效果

折叠时的视图

在这里插入图片描述
展开时的视图
在这里插入图片描述

悬挂式 Notification

  • Android 5.0
  • 又称横幅通知
  • 悬挂在屏幕上方,自动消失
  • 可能由于手机系统原因,官方推荐的方式不适用,目前未解决

Notification 显示等级

Android 5.0

VISIBILITY_PUBLIC:任何情况都会显示通知
VISIBILITY_PRIVATE:只有在没有锁屏时会显示通知
VISIBILITY_SECRET:在 pin、password 等安全锁和没有锁屏的情况下才能够显示通知

使用方法

 Notification.Builder builder;
 builder.setVisibility(Notification.VISIBILITY_PUBLIC);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值