一、基本使用
1. 效果图
2. 使用 2.1 首先需要一个 NotificationManager 来进行管理,可以调用 Context 的 getSystemService 方法获取,这里传入一个 Context
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
2.2 需要使用一个 Builder 构造器来创建 Notification 对象,由于 API 不同会造成不同版本的通知出现不稳定的问题, 所以这里使用 NotificationCompat 类来兼容各个版本。
Notification notification = new NotificationCompat.Builder(context, "order")
2.3 基本设置
Notification notification = new NotificationCompat.Builder(context, "order")
.setContentTitle(title) // 设置标题
.setContentText(content) // 设置内容
.setWhen(System.currentTimeMillis()) // 设置时间
.setSmallIcon(R.mipmap.ic_launcher) // 设置小图标
.setColor(Color.parseColor("#F00606")) // 设置红色
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) // 设置大图标
.build();
2.4 调用 notify() 让通知显示出来(第一个参数是 ID, 保证每个通知所指定的 id 都是不同的,第二个参数是 notification 对象)
manager.notify(1, notification);
二、进阶使用
- 实现点击通知实现跳转;
- 设置通知为自动取消;
- 设置通知提示音;
- 设置振动;
- 设置前置LED灯进行闪烁;
- 完整代码
/**
* Created on 2021/12/28 14:15
*
* @author Gong Youqiang
*/
public class Utils {
public static void sendNotification(Context context,String title,String content) {
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "order";
String channelName = "消息";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(context,channelId, channelName, importance);
}
Intent intent=new Intent(context, MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);
Notification notification = new NotificationCompat.Builder(context, "order")
.setAutoCancel(true)
.setContentTitle(title) // 设置标题
.setContentText(content) // 设置内容
.setWhen(System.currentTimeMillis()) // 设置时间
.setSmallIcon(R.mipmap.ic_launcher) // 设置小图标
//设置红色
.setColor(Color.parseColor("#F00606"))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) // 设置大图标
.setContentIntent(pendingIntent)
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) //设置通知提示音
.setVibrate(new long[]{0,1000,1000,1000}) //设置振动, 需要添加权限
.setLights(Color.GREEN,1000,1000)//设置前置LED灯进行闪烁, 第一个为颜色值 第二个为亮的时长 第三个为暗的时长
.build();
manager.notify(1, notification);
}
@TargetApi(Build.VERSION_CODES.O)
private static void createNotificationChannel(Context context,String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
}
------------------------------------使用------------------------------------------
public void sendNotification(View view) {
Utils.sendNotification(this,"新工单提醒","您有新的工单消息,请前往查看");
}
三、关于 Android8.0 通知栏适配
封装代码
public class NotificationUtils extends ContextWrapper {
public static final String CHANNEL_ID = "default";
private static final String CHANNEL_NAME = "Default_Channel";
private NotificationManager mManager;
private int[] flags;
public NotificationUtils(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//android 8.0以上需要特殊处理,也就是targetSDKVersion为26以上
createNotificationChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
//第一个参数:channel_id
//第二个参数:channel_name
//第三个参数:设置通知重要性级别
//注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;
//范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
channel.canBypassDnd();//是否绕过请勿打扰模式
channel.enableLights(true);//闪光灯
channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
channel.setLightColor(Color.RED);//闪关灯的灯光颜色
channel.canShowBadge();//桌面launcher的消息角标
channel.enableVibration(true);//是否允许震动
channel.getAudioAttributes();//获取系统通知响铃声音的配置
channel.getGroup();//获取通知取到组
channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
channel.shouldShowLights();//是否会有灯光
getManager().createNotificationChannel(channel);
}
/**
* 获取创建一个NotificationManager的对象
* @return NotificationManager对象
*/
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
/**
* 清空所有的通知
*/
public void clearNotification(){
getManager().cancelAll();
}
/**
* 建议使用这个发送通知
* 调用该方法可以发送通知
* @param notifyId notifyId
* @param title title
* @param content content
*/
public void sendNotification(int notifyId, String title, String content , int icon) {
Notification build;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//android 8.0以上需要特殊处理,也就是targetSDKVersion为26以上
//通知用到NotificationCompat()这个V4库中的方法。但是在实际使用时发现书上的代码已经过时并且Android8.0已经不支持这种写法
Notification.Builder builder = getChannelNotification(title, content, icon);
build = builder.build();
} else {
NotificationCompat.Builder builder = getNotificationCompat(title, content, icon);
build = builder.build();
}
if (flags!=null && flags.length>0){
for (int a=0 ; a0){
for (int a=0 ; a= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
} else {
//注意用下面这个方法,在8.0以上无法出现通知栏。8.0之前是正常的。这里需要增强判断逻辑
builder = new NotificationCompat.Builder(getApplicationContext());
builder.setPriority(PRIORITY_DEFAULT);
}
builder.setContentTitle(title);
builder.setContentText(content);
builder.setSmallIcon(icon);
builder.setPriority(priority);
builder.setOnlyAlertOnce(onlyAlertOnce);
builder.setOngoing(ongoing);
if (remoteViews!=null){
builder.setContent(remoteViews);
}
if (intent!=null){
builder.setContentIntent(intent);
}
if (ticker!=null && ticker.length()>0){
builder.setTicker(ticker);
}
if (when!=0){
builder.setWhen(when);
}
if (sound!=null){
builder.setSound(sound);
}
if (defaults!=0){
builder.setDefaults(defaults);
}
//点击自动删除通知
builder.setAutoCancel(true);
return builder;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private Notification.Builder getChannelNotification(String title, String content, int icon){
Notification.Builder builder = new Notification.Builder(getApplicationContext(), CHANNEL_ID);
Notification.Builder notificationBuilder = builder
//设置标题
.setContentTitle(title)
//消息内容
.setContentText(content)
//设置通知的图标
.setSmallIcon(icon)
//让通知左右滑的时候是否可以取消通知
.setOngoing(ongoing)
//设置优先级
.setPriority(priority)
//是否提示一次.true - 如果Notification已经存在状态栏即使在调用notify函数也不会更新
.setOnlyAlertOnce(onlyAlertOnce)
.setAutoCancel(true);
if (remoteViews!=null){
//设置自定义view通知栏
notificationBuilder.setContent(remoteViews);
}
if (intent!=null){
notificationBuilder.setContentIntent(intent);
}
if (ticker!=null && ticker.length()>0){
//设置状态栏的标题
notificationBuilder.setTicker(ticker);
}
if (when!=0){
//设置通知时间,默认为系统发出通知的时间,通常不用设置
notificationBuilder.setWhen(when);
}
if (sound!=null){
//设置sound
notificationBuilder.setSound(sound);
}
if (defaults!=0){
//设置默认的提示音
notificationBuilder.setDefaults(defaults);
}
if (pattern!=null){
//自定义震动效果
notificationBuilder.setVibrate(pattern);
}
return notificationBuilder;
}
private boolean ongoing = false;
private RemoteViews remoteViews = null;
private PendingIntent intent = null;
private String ticker = "";
private int priority = Notification.PRIORITY_DEFAULT;
private boolean onlyAlertOnce = false;
private long when = 0;
private Uri sound = null;
private int defaults = 0;
private long[] pattern = null;
/**
* 让通知左右滑的时候是否可以取消通知
* @param ongoing 是否可以取消通知
* @return
*/
public NotificationUtils setOngoing(boolean ongoing){
this.ongoing = ongoing;
return this;
}
/**
* 设置自定义view通知栏布局
* @param remoteViews view
* @return
*/
public NotificationUtils setContent(RemoteViews remoteViews){
this.remoteViews = remoteViews;
return this;
}
/**
* 设置内容点击
* @param intent intent
* @return
*/
public NotificationUtils setContentIntent(PendingIntent intent){
this.intent = intent;
return this;
}
/**
* 设置状态栏的标题
* @param ticker 状态栏的标题
* @return
*/
public NotificationUtils setTicker(String ticker){
this.ticker = ticker;
return this;
}
/**
* 设置优先级
* 注意:
* Android 8.0以及上,在 NotificationChannel 的构造函数中指定,总共要五个级别;
* Android 7.1(API 25)及以下的设备,还得调用NotificationCompat 的 setPriority方法来设置
*
* @param priority 优先级,默认是Notification.PRIORITY_DEFAULT
* @return
*/
public NotificationUtils setPriority(int priority){
this.priority = priority;
return this;
}
/**
* 是否提示一次.true - 如果Notification已经存在状态栏即使在调用notify函数也不会更新
* @param onlyAlertOnce 是否只提示一次,默认是false
* @return
*/
public NotificationUtils setOnlyAlertOnce(boolean onlyAlertOnce){
this.onlyAlertOnce = onlyAlertOnce;
return this;
}
/**
* 设置通知时间,默认为系统发出通知的时间,通常不用设置
* @param when when
* @return
*/
public NotificationUtils setWhen(long when){
this.when = when;
return this;
}
/**
* 设置sound
* @param sound sound
* @return
*/
public NotificationUtils setSound(Uri sound){
this.sound = sound;
return this;
}
/**
* 设置默认的提示音
* @param defaults defaults
* @return
*/
public NotificationUtils setDefaults(int defaults){
this.defaults = defaults;
return this;
}
/**
* 自定义震动效果
* @param pattern pattern
* @return
*/
public NotificationUtils setVibrate(long[] pattern){
this.pattern = pattern;
return this;
}
/**
* 设置flag标签
* @param flags flags
* @return
*/
public NotificationUtils setFlags(int... flags){
this.flags = flags;
return this;
}
}