一般用法,兼容Android O
//第一步:获取状态通知栏管理:
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
//第二步:实例化通知栏构造器NotificationCompat.Builder:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
NotificationChannel mChannel = new NotificationChannel(id, name,
NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(MainActivity.this, id)
.setContentTitle("这是一个内容标题")//设置通知栏标题
.setContentText("这是一个内容文本") //设置通知栏显示内容
.setWhen(System.currentTimeMillis())//通知产生的时间。
// 会在通知信息里显示,通常是系统获取到的时间
.setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher))//设置通知大ICON
.build();
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MainActivity.this, id)
.setContentTitle("这是一个内容标题")
.setContentText("这是一个内容文本")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher));
// .setOngoing(true);
notification = notificationBuilder.build();
}
//第三步:对Builder进行配置:
manager.notify(1, notification);
参考:
【Android】23.0 手机多媒体(一)——通知(Notification)(1):状态栏显示通知
【Android】24.0 手机多媒体(二)——通知(Notification)(2):状态栏通知点击事件处理
参数
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.canBypassDnd();//可否绕过请勿打扰模式
notificationChannel.enableLights(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//锁屏显示通知
notificationChannel.setLightColor(Color.RED);//指定闪光时的灯光颜色
notificationChannel.canShowBadge();//桌面laucher显示角标
notificationChannel.enableVibration(true);//是否允许震动
notificationChannel.getAudioAttributes();//获取系统通知响铃
notificationChannel.getGroup();//获取通知渠道组
notificationChannel.setVibrationPattern(new long[]{100, 100, 200});
notificationChannel.shouldShowLights();//是否会闪光灯
manager.createNotificationChannel(notificationChannel);
Notification notification = Notification.Builder(this)
.setAutoCancel(true)
.setChannelId("channel_id")
.setContentTitle("新消息来来")
.setContentText("明天放假!")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
//.setStyle(new Notification.BigTextStyle())
manager.notify(1,builder.build());
更新进度
builder创建同上
builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
manager.notify(2, builder.build());
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(1000);
builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);
builder.setProgress(100, i, false);
manager.notify(2, builder.build());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
可跳转的通知
/**
* 发送一个点击跳转到MainActivity的消息
*/
private void sendSimplestNotificationWithAction() {
//获取PendingIntent
Intent mainIntent = new Intent(this, MainActivity.class);
PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
//第二步:实例化通知栏构造器NotificationCompat.Builder:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
NotificationChannel mChannel = new NotificationChannel(id, name,
NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(MainActivity.this, id)
.setContentTitle("这是一个内容标题")//设置通知栏标题
.setContentText("这是一个内容文本") //设置通知栏显示内容
.setWhen(System.currentTimeMillis())//通知产生的时间。
// 会在通知信息里显示,通常是系统获取到的时间
.setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher))//设置通知大ICON
.setContentIntent(mainPendingIntent)
.build();
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MainActivity.this, id)
.setContentTitle("这是一个内容标题")
.setContentText("这是一个内容文本")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher));
// .setOngoing(true);
notification = notificationBuilder.build();
}
//发送通知
notifyManager.notify(3, notification);
}
自定义通知布局(未完善)
/**
* 自定义通知栏布局
*/
private void sendCustomNotification(){
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_big_layout);
//remoteViews.setTextViewText(R.id.notification_title,"custom_title");
//remoteViews.setTextViewText(R.id.notification_content,"custom_content");
//即将要发生的意图 他是可以被取消 更新
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//remoteViews.setOnClickPendingIntent(R.id.turn_next, pendingIntent);
//remoteViews.setOnClickFillInIntent(R.id.turn_next, intent);
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
//第二步:实例化通知栏构造器NotificationCompat.Builder:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
NotificationChannel mChannel = new NotificationChannel(id, name,
NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(MainActivity.this, id)
.setContentTitle("这是一个内容标题")//设置通知栏标题
.setContentText("这是一个内容文本") //设置通知栏显示内容
.setWhen(System.currentTimeMillis())//通知产生的时间。
// 会在通知信息里显示,通常是系统获取到的时间
.setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher))//设置通知大ICON
.setCustomContentView(remoteViews)
.build();
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(MainActivity.this, id)
.setContentTitle("这是一个内容标题")
.setContentText("这是一个内容文本")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher))
.setCustomContentView(remoteViews);
// .setOngoing(true);
notification = notificationBuilder.build();
}
manager.notify(3,notification);
}
通知栏Notification点击,取消,清除响应事件(未实现,通知栏的处理还是有很多内容!有时间再搞)
通知栏Notification点击,取消,清除响应事件
跳转通知栏设置中心
/**
* 前往设置中心
*
* @param activity 上下文
* @param requestCode 请求码 用来传给startActivityForResult作为回调标识符,不需要回调处理的不用设置,跳转直接用startActivity即可
*/
public void startAppSetting(Activity activity, int requestCode) {
try {
// 根据isOpened结果,判断是否需要提醒用户跳转AppInfo页面,去打开App通知权限
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//这种方案适用于 API 26, 即8.0(含8.0)以上可以用
intent.putExtra(EXTRA_APP_PACKAGE, activity.getPackageName());
intent.putExtra(EXTRA_CHANNEL_ID, activity.getApplicationInfo().uid);
//这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用
intent.putExtra("app_package", activity.getPackageName());
intent.putExtra("app_uid", activity.getApplicationInfo().uid);
} else {
//4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
intent.putExtra("com.android.settings.ApplicationPkgName", activity.getPackageName());
}
}
activity.startActivityForResult(intent,requestCode);
} catch (Exception e) {
e.printStackTrace();
// 出现异常则跳转到应用设置界面:锤子坚果3——OC105 API25
Intent intent = new Intent();
//下面这种方案是直接跳转到当前应用的设置界面。
//https://blog.csdn.net/ysy950803/article/details/71910806
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivityForResult(intent,requestCode);
}
}
参考:
安卓8.0后通知栏Notification【判断APP通知栏权限是否开启,以及关闭了通知权限后设置或者自定义通知栏消息显示】