这是Android 通知相关的内容的总结
android中通知用到的地方很多,经常有的例如推送消息,下载时的提示等。
使用new Notification()方式创建通知:
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity( this, 0, new Intent(this, Main2Activity.class), 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);
mNotifyManager .notify(NOTIFICATIONS_ID, notification);
Android 3.0开始弃用new Notification()方式,改用Notification.Builder()来创建通知:
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity( this, 0, new Intent(this, Main2Activity.class), 0);
Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My Title")
.setContentText("ContentText")
.setContentIntent(contentIntent) .build();
mNotifyManager.notify(NOTIFICATIONS_ID, notification);
为了兼容API level 11之前的版本,v4 Support Library中提供了
NotificationCompat.Builder()这个替代方法。
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity( this, 0, new Intent(this, Main2Activity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My Title")
.setContentText("ContentText")
.setContentIntent(contentIntent);
mNotifyManager.notify(NOTIFICATIONS_ID, mBuilder.build());
Intent hangIntent = new Intent(context, Main2Activity.class);
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.timg)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.timg))
.setAutoCancel(true)
.setSubText(subText + "getNormalNotification")
.setTicker(ticker)
.setShowWhen(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(intent)
.setContentText(content)
.setContentTitle(title);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(1, notification);
private NotificationManager getManager() {
if (manager == null) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
return manager;
}
Intent hangIntent = new Intent(context, Main2Activity.class);
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.timg)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.timg))
.setAutoCancel(true)
.setTicker(ticker)
.setContentIntent(intent)
.setContentText(content + "getDownloadNotification")
.setContentTitle(title);
new Thread(new Runnable() {
@Override
public void run() {
int progress;
for (progress = 0; progress <= 100; progress += 5) {
builder.setProgress(100, progress, false);
builder.setContentInfo(progress + "%");
getManager().notify(1, builder.build());
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("下载完成")
.setProgress(0, 0, false).setContentInfo("");
getManager().notify(1, builder.build());
}
}).start();
Intent hangIntent = new Intent(context, Main2Activity.class);
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.timg)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.timg))
.setAutoCancel(true)
.setTicker(ticker)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL);
NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
//bigText 给样式设置大文本内容
style.bigText("正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文");
//setBigContentTitle 给样式设置大文本的标题
style.setBigContentTitle("标题");
//SummaryText没什么用 可以不设置
style.setSummaryText("末尾的文字内容");
builder.setStyle(style).setContentIntent(intent)
.setContentText(content)
.setContentTitle(title);
getManager().notify(1, notification);
Intent hangIntent = new Intent(context, Main2Activity.class);
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.timg)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.timg))
.setAutoCancel(true)
.setTicker(ticker)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL);
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.setBigContentTitle("BigContentTitle");
style.setSummaryText("SummaryText");
style.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.title));
builder.setStyle(style).setContentIntent(intent)
.setContentText(content)
.setContentTitle(title);
getManager().notify(1, notification);
Intent hangIntent = new Intent();
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
hangIntent.setClass(context, Main2Activity.class);
PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.timg)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.timg))
.setAutoCancel(true)
.setVisibility(VISIBILITY_PUBLIC)
.setFullScreenIntent(hangPendingIntent, true)
.setContentText(content)
.setContentTitle(title);
getManager().notify(1, notification);
Android 5.0(API level 21)开始,通知可以显示在锁屏上。用户可以通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。
你的应用可以通过setVisibility()控制通知的显示等级:
VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容
VISIBILITY_PUBLIC : 显示通知的全部内容
VISIBILITY_SECRET : 不显示任何内容,包括图标
查看官方文档 https://developer.android.com/preview/features/notification-channels.html
文档里是这样写的:
If you targetAndroid O and post a notification without specifying a valid notificationschannel, the notification fails to post and the system logs an error.
大意就是说,如果在Android O发送通知需要设置notificationchannel,否则通知会发送失败。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("通知渠道ID",
"通知渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true); //设置开启指示灯,如果设备有的话
channel.setLightColor(Color.RED); //设置指示灯颜色
channel.setShowBadge(true); //设置是否显示角标
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);//设置是否应在锁定屏幕上显示此频道的通知
channel.setDescription("通知渠道描述");//设置渠道描述
channel.setVibrationPattern(new long[]{100,200,300,400,500,600});//设置震动频率
channel.setBypassDnd(true);//设置是否绕过免打扰模式
mNotificationManager.createNotificationChannel(channel);
Intent hangIntent = new Intent(context, Main2Activity.class);
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.timg)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.timg))
.setAutoCancel(true)
.setSubText(subText + "getNormalNotification")
.setTicker(ticker)
.setShowWhen(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(intent)
.setContentText(content)
.setContentTitle(title);
getManager().notify(1, notification);
锁屏通知,悬挂通知等测试时记得要在手机里开启相关权限,很多手机权限默认关闭
参考资料:
Android Notification 通知样式总结
https://shoewann0402.github.io/2016/05/12/Android-Notification-%E9%80%9A%E7%9F%A5%E6%A0%B7%E5%BC%8F%E6%80%BB%E7%BB%93/
Android通知栏介绍与适配总结
https://blog.csdn.net/jiankeufo/article/details/77977564