8.0以上系统创建通知栏
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String title, String message) {
String CHANNEL_ID = "channel_01";
String CHANNEL_NAME = "channel_name";
//设置渠道
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
//设置跳转的页面
PendingIntent intent = PendingIntent.getActivity(this,
100, new Intent(this, MainActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_kuda_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_kuda_launcher))
.setAutoCancel(true)
.build();
manager.notify(2, notification);
}
8.0以下系统创建通知栏
public void createNotification(String title, String message){
//创建通知管理类
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//设置跳转的页面
PendingIntent intent = PendingIntent.getActivity(this,
100, new Intent(this, MainActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
//创建通知建设类
Notification.Builder builder = new Notification.Builder(this)
.setContentTitle(title)//设置通知栏标题
.setContentText(message)//设置通知栏内容
.setContentIntent(intent)//设置跳转
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_kuda_launcher)//设置图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_kuda_launcher))
.setDefaults(Notification.DEFAULT_ALL)//设置
.setAutoCancel(true);//设置自动消失
//创建通知类
Notification notification = builder.build();
//显示在通知栏
manager.notify(2, notification);
}