一、工具类
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.drawable.push_small)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(id++, notification);
}
}
二、使用
// 初始化通知类
NotificationUtils notificationUtils = NotificationUtils.getInstance();
notificationUtils.init(getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 8.0以上创建通道
// 当然这里是具体按照项目需要设定的通道类型创建
notificationUtils.createNotificationChannel("chat", "聊天", NotificationManager.IMPORTANCE_HIGH);
}
// 设置Intent相关数据
Intent intent = new Intent();
intent.setClass(this, Activity.class);
notificationUtils.sendNotification("chat", "标题", data, intent);