android notification

GLGJing’s Blog: Android 开发之 Notification 详解 vipra:Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

RemoteViews

HuDP Android开发艺术探索 第5章 理解RemoteViews 读书笔记 _风_的专栏 android remoteView原理

通知的基础用法

浮动通知


NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

builder.setSmallIcon(R.drawable.notification_icon)
builder.setContentTitle("My notification")
builder.setContentText("Hello World!");
// 设置通知的优先级
builder.setPriority(NotificationCompat.PRIORITY_MAX);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// 设置通知的提示音
builder.setSound(alarmSound);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, builder.build());        

setVisibility()

扩展样式 setStyle() BigPictureStyle,BigTextStyle,InboxStyle

自定义布局样式 setContent()


RemoteViews bigView;
RemoteViews smallView;

// 构建 bigView 和 smallView。
...

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

// 设置自定义 RemoteViews
builder.setContent(smallView).setSmallIcon(R.drawable.icon_notification);
Notification notification = builder.build();

// 如果系统版本 >= Android 4.1,设置大视图 RemoteViews
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  notification.bigContentView = bigView;
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(DAILY_PUSH_NOTIFICATION_ID, notification);