版本适配
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
- 创建 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);
- 给 Notification 添加各种属性
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.setContentTitle("普通通知");
- 显示通知
Notification notification = builder.build();
notificationManager.notify(1, notification);
- 运行效果
折叠式 Notification
目前只测试了 Android N 7.0 (API 24)以上
- 展开的布局文件
<?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>
- 创建 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);
- 给 Notification 添加各种属性
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.setContentTitle("折叠式通知");
- 设置自定义的视图后并显示通知,如果不自定义折叠时的视图,普通状态下和普通 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);
- 运行效果
折叠时的视图
展开时的视图
悬挂式 Notification
- Android 5.0
- 又称横幅通知
- 悬挂在屏幕上方,自动消失
- 可能由于手机系统原因,官方推荐的方式不适用,目前未解决
Notification 显示等级
Android 5.0
VISIBILITY_PUBLIC:任何情况都会显示通知
VISIBILITY_PRIVATE:只有在没有锁屏时会显示通知
VISIBILITY_SECRET:在 pin、password 等安全锁和没有锁屏的情况下才能够显示通知
使用方法
Notification.Builder builder;
builder.setVisibility(Notification.VISIBILITY_PUBLIC);