封装Notification工具类,主要是发送通知,检查通知是否开启,消息渠道是否开启
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import static android.content.Context.NOTIFICATION_SERVICE;
/**
* 单例通知类
* 适配Android O
*/
public class NotificationUtils {
private Context context;
private NotificationManager notificationManager;
private static int id = 0;
private static class NotificationUtilsHolder {
public static final NotificationUtils notificationUtils = new NotificationUtils();
}
private NotificationUtils(){
}
public static NotificationUtils getInstance() {
return NotificationUtilsHolder.notificationUtils;
}
/**
* 初始化
* @param context 引用全局上下文
*/
public void init(Context context) {
this.context = context;
notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
}
/**
* 创建通知通道
* @param channelId 通道id
* @param channelName 通道名称
* @param importance 通道级别
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
notificationManager.createNotificationChannel(channel);
}
/**
* 发送通知
* @param channelId 通道id
* @param title 标题
* @param content 内容
* @param intent 意图
*/
public void sendNotification(String channelId, String title, String content, Intent intent) {
PendingIntent pendingIntent = null;
if (intent != null) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
Notification notification = new NotificationCompat.Builder(context, channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(id++, notification);
}
/**
* 判断通知是否开启(非单个消息渠道)
* @param context 上下文
* @return true 开启
* API19 以上可用
*/
public static boolean checkNotificationsEnabled(Context context) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();
return areNotificationsEnabled;
}
/**
* 判断通知渠道是否开启(单个消息渠道)
* @param context 上下文
* @param channelID 渠道 id
* @return true 开启
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public static boolean checkNotificationsChannelEnabled(Context context, String channelID) {
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (manager == null) {
return false;
}
NotificationChannel channel = manager.getNotificationChannel(channelID);
return !(channel.getImportance() == NotificationManager.IMPORTANCE_NONE);
}
}
MyApplication中初始化并创建通道
// 初始化通知类
NotificationUtils notificationUtils = NotificationUtils.getInstance();
notificationUtils.init(getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 8.0以上创建通道
// 当然这里是具体按照项目需要设定的通道类型创建
notificationUtils.createNotificationChannel("chat", "聊天", NotificationManager.IMPORTANCE_HIGH);
}
在需要发送通知的地方调用
NotificationUtils notificationUtils = NotificationUtils.getInstance();
// 设置Intent相关数据
Intent intent = new Intent(this, MainActivity.class);
notificationUtils.sendNotification("chat","无线电通知", "长江长江,我是黄河", intent);
// 判断通知是否开启
boolean isOpen = NotificationUtils.checkNotificationsEnabled(context);
// 判断某个通知渠道是否开启
boolean isOpen = NotificationUtils.checkNotificationsChannelEnabled(context, "chat");
// 跳转设置开启
if(!isOpen) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
startActivity(intent);
}