Android Notification 通知样式总结

Notification 通知系统可让用户随时了解应用中的相关和即时事件,例如来自好友的新聊天信息或日历事件。可将通知视作新闻频道,在重要的事件发生时提醒用户注意,或者当作日志,在用户未注意时记录事件—可在用户的所有 Android 设备上按需同步。随着系统版本的更新,通知栏的风格种类得到扩展,从2.x、4.x、5.x、6.x,以至于最新版本的7.x(Android N)都有相当大的变化,那么现在来总结一下在Android 5.x(Lollipop)- Android 6.x(Mashmallow)上的通知栏样式,对于Android N 上的通知栏风格,我们后面在谈。

1
2
3
4
5
public static class
Notification.Builder
extends Object
java.lang.Object
↳ android.app.Notification.Builder

在项目初始化的时候会,创建好通知栏管理对象—— NotificationManager

1
NotificationManager manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

NormalStyle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//为了版本兼容  选择V7包下的NotificationCompat进行构造
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//setTicker 在5.0以上不显示Ticker属性信息
builder.setTicker("状态栏显示的提示");
//setContentTitle 通知栏通知的标题
builder.setContentTitle("内容标题");
//setContentText 通知栏通知的详细内容
builder.setContentText("内容文本信息");
//setAutoCancel 点击通知的清除按钮是否清除该消息(true/false
builder.setAutoCancel(true);
//setLargeIcon 通知消息上的大图标
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
//setSmallIcon 通知上面的小图标
builder.setSmallIcon(R.mipmap.ic_launcher);//小图标
//创建一个意图
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
//setContentIntent 将意图设置到通知上
builder.setContentIntent(pIntent);
//通知默认的声音 震动 呼吸灯
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
//构建通知
Notification notification = builder.build();
//将构建好的通知添加到通知管理器中,执行通知
manger.notify(0, notification);

normal

DownloadStyle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//为了版本兼容  选择V7包下的NotificationCompat进行构造
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//setSmallIcon 通知上面的小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//setLargeIcon 通知消息上的大图标
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
//setAutoCancel 点击通知的清除按钮是否清除该消息(true/false
builder.setAutoCancel(true);
builder.setContentTitle("微信");
builder.setContentText("下载中");
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr += 5) {
builder.setProgress(100, incr, false);
builder.setContentInfo(incr + "%");
manger.notify(1, builder.build());
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("下载完成")
// 移除进度条
.setProgress(0, 0, false).setContentInfo("");
Notification notification = builder.build();
manger.notify(1, notification);
}
}
).start();

downloadtype1

downloadfinish

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//为了版本兼容  选择V7包下的NotificationCompat进行构造
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//setSmallIcon 通知上面的小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//setLargeIcon 通知消息上的大图标
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
//setAutoCancel 点击通知的清除按钮是否清除该消息(true/false
builder.setAutoCancel(true);
builder.setContentTitle("微信");
builder.setContentText("下载中");
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr += 5) {
builder.setProgress(0, 0, true);
manger.notify(2, builder.build());
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("下载完成")
// 移除进度条
.setProgress(0, 0, false).setContentInfo("");
Notification notification = builder.build();
manger.notify(2, notification);
}
}
).start();

downloadtype1

downloadfinish

BigTextStyle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("BigTextStyle");
builder.setContentText("BigTextStyle演示示例");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
android.support.v4.app.NotificationCompat.BigTextStyle style = new android.support.v4.app.NotificationCompat.BigTextStyle();
//bigText 给样式设置大文本内容
style.bigText("这里是点击通知后要显示的正文,正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字正文很多字");
//setBigContentTitle 给样式设置大文本的标题
style.setBigContentTitle("点击后的标题");
//SummaryText没什么用 可以不设置
style.setSummaryText("末尾只一行的文字内容");
//setStyle 将样式添加到通知
builder.setStyle(style);
builder.setAutoCancel(true);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manger.notify(3, notification);

bigtextStyle1

bigtextStyle2

InBoxStyle

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("InboxStyle");
builder.setContentText("InboxStyle演示示例");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
android.support.v4.app.NotificationCompat.InboxStyle style = new android.support.v4.app.NotificationCompat.InboxStyle();
style.setBigContentTitle("BigContentTitle")
        .addLine("啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦")
        .addLine("哈哈哈哈哈")
        .addLine("嘻嘻嘻嘻")
        .addLine("嘿嘿嘿")
        .addLine("咦咦咦咦")
        .setSummaryText("SummaryText");
builder.setStyle(style);
builder.setAutoCancel(true);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manger.notify(4, notification);

inBoxStyle1

inBoxStyle2

BigPictureStyle

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("BigPictureStyle");
builder.setContentText("BigPicture演示示例");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
android.support.v4.app.NotificationCompat.BigPictureStyle style = new android.support.v4.app.NotificationCompat.BigPictureStyle();
style.setBigContentTitle("BigContentTitle");
style.setSummaryText("SummaryText");
style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.head));
builder.setStyle(style);
builder.setAutoCancel(true);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manger.notify(5, notification);

bigPictureStyle1

bigPictureStyle2

HungUpStyle

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("横幅通知");
builder.setContentText("请在设置通知管理中开启消息横幅提醒权限");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setFullScreenIntent(pIntent, true);
builder.setAutoCancel(true);
Notification notification = builder.build();
manger.notify(6, notification);

hungUpStyle

MediaStyle

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("You Are Not Alone");
builder.setContentText("Michael Jackson");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
//在锁定屏幕上显示控制,即使用户隐藏敏感内容(API>=21)
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
//addAction: 3个参数 图标、图标的标题、图标的动作
builder.addAction(android.R.drawable.ic_media_previous, "上一曲", null);
builder.addAction(android.R.drawable.ic_media_pause, "暂停", null);
builder.addAction(android.R.drawable.ic_media_play, "播放", pIntent);
builder.addAction(android.R.drawable.ic_media_next, "下一曲", null);
NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();
style.setMediaSession(new MediaSessionCompat(this, "MediaSession",
        new ComponentName(MainActivity.this, Intent.ACTION_MEDIA_BUTTON), null).getSessionToken());
//CancelButton在5.0以下的机器有效
style.setCancelButtonIntent(pIntent);
style.setShowCancelButton(true);
//设置要现实在通知右方的图标 最多三个
style.setShowActionsInCompactView(2, 3);
builder.setStyle(style);
builder.setShowWhen(false);
Notification notification = builder.build();
manger.notify(7, notification);

mediaStyle1

mediaStyle2

RemoteViewsStyle

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);//小图标
RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.remoteview_main);
String title="Android Developer";
String content="developer.android.com";
remoteViews.setTextViewText(R.id.tv_title, title);
remoteViews.setTextViewText(R.id.tv_content, content);
builder.setContent(remoteViews);
Notification notification = builder.build();
manger.notify(8, notification);    

remoteViewsStyle

0%