版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28779083/article/details/87935210
8.0系统的通知栏适配
做了一个工具类,直接复制过去用吧。注释都比较清晰了。
1、在Activity中创建通知渠道
NotificationUtils.init().createNotificationChannel(this)
2、发送通知
//pushModel:自定义的消息数据model
NotificationUtils.init().sendNotify(context, pushModel, "1", "1");
PushModel ,按需修改成自己的
public class PushModel {
private String title;
private String content;
private String id;
private String type;
private String logid;
private String image;
private String url;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLogid() {
return logid;
}
public void setLogid(String logid) {
this.logid = logid;
}
}
3、工具类
/**
* android通知栏,适配8.0
*
* @author mtj
* @data 2019-2-26 14:38:51
*/
public class NotificationUtils {
private static NotificationUtils notificationUtils;
public static NotificationUtils init() {
if (notificationUtils == null) {
notificationUtils = new NotificationUtils();
}
return notificationUtils;
}
/**
* 适配8.0通知栏
* 在启动页或首页中创建通知渠道
*/
public void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//通知渠道
String channelId = "systemmsg";
String channelName = "系统消息";
int importance = NotificationManager.IMPORTANCE_HIGH;
setNotificationChannel(context, channelId, channelName, importance);
}
}
@TargetApi(Build.VERSION_CODES.O)
private void setNotificationChannel(Context context, String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
//设置不显示角标
channel.setShowBadge(false);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
/**
* 发送通知栏
*
* @param context
* @param model 自定义消息model,修改成自己的
* @param sound 0:不响铃 1 :响铃
* @param vibrate 0:不震动 1 :震动
*/
public void sendNotify(Context context, PushModel model, String sound, String vibrate) {
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//8.0及以上,需传入渠道的id
Notification notification = new NotificationCompat.Builder(context, "systemmsg")
.setAutoCancel(true)
.setContentTitle(model.getTitle())
.setContentText(model.getContent())
.setSmallIcon(R.drawable.set_about_ico)
.setLargeIcon(
BitmapFactory.decodeResource(context.getResources(),
R.drawable.set_about_ico))
.setWhen(System.currentTimeMillis())
.build();
notification.contentIntent = PendingIntent.getActivity(context, 0,
getIntent(model.getType()), PendingIntent.FLAG_UPDATE_CURRENT);
notification = setAlarmParams(context, notification, sound, vibrate);
manager.notify(1, notification);
Log.i("mtj", "进入8.0通知:" + model.getContent());
} else {
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle(model.getTitle())
.setContentText(model.getContent())
.setSmallIcon(R.drawable.set_about_ico)
.setLargeIcon(
BitmapFactory.decodeResource(context.getResources(),
R.drawable.set_about_ico))
.setWhen(System.currentTimeMillis())
.build();
notification.contentIntent = PendingIntent.getActivity(context, 0,
getIntent(model.getType()), PendingIntent.FLAG_UPDATE_CURRENT);
notification = setAlarmParams(context, notification, sound, vibrate);
manager.notify(1, notification);
Log.i("mtj", "进入8.0以下通知:" + model.getContent());
}
}
/**
* 根据消息type自定义通知动作
*
* @return
*/
private Intent getIntent(String type) {
Intent intent = new Intent();
if (!TextUtils.isEmpty(type)) {
switch (TurnsUtils.getInt(type, 0)) {
case -1:
break;
default:
break;
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
return intent;
}
/**
* 新消息通知模式
*
* @param context
* @param notification
* @return
*/
private Notification setAlarmParams(Context context, Notification notification, String sound, String vibrate) {
AudioManager volMgr = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
switch (volMgr.getRingerMode()) {// 获取系统设置的铃声模式
case AudioManager.RINGER_MODE_SILENT:// 静音模式,值为0,这时候不震动,不响铃
notification.sound = null;
notification.vibrate = null;
break;
case AudioManager.RINGER_MODE_VIBRATE:// 震动模式,值为1,这时候震动,不响铃
if (vibrate.equals("1")) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.sound = null;
} else {
notification.sound = null;
notification.vibrate = null;
}
break;
case AudioManager.RINGER_MODE_NORMAL:// 常规模式,值为2,分两种情况:1_响铃但不震动,2_响铃+震动
if (sound.equals("1") && vibrate.equals("1")) {// 声音+震动
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
} else if (sound.equals("0") && vibrate.equals("1")) {// 震动 ,不响铃
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.sound = null;
} else if (sound.equals("1") && vibrate.equals("0")) {// 响铃,不震动
notification.defaults |= Notification.DEFAULT_SOUND;
notification.vibrate = null;
} else {// 静音
notification.sound = null;
notification.vibrate = null;
}
break;
default:
break;
}
return notification;
}
}