package union.com.viewgroup.utils;
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.graphics.Color;
import android.os.Build;
import android.util.Log;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import union.com.viewgroup.R;
import union.com.viewgroup.ui.MainActivity;
import static androidx.core.app.NotificationCompat.BADGE_ICON_SMALL;
/*
* @Author xiaobo
* @Date 2019/9/19 11:39
* @Version 1.0
* @Desc 通知栏管理类
*/
public class NotificationManagerUtils {
/**
* 通道Id
*/
private static final String CHANNEL_ID = "channel.id";
/**
* 通知Id
*/
private static final int NOTIFICATION_ID = 1001;
/**
* 点击通知需要跳转到的Activity
*/
private Class<?> aClass = MainActivity.class;
/**
* 日志Tag
*/
private static final String TAG = NotificationManagerUtils.class.getSimpleName();
/**
* 通知管理器
*/
private NotificationManager manager;
private static NotificationManagerUtils instance;
private Notification notification;
private NotificationManagerUtils() {
}
/**
* 获取NotificationManagerUtils 实例
* @return NotificationManagerUtils
*/
public static NotificationManagerUtils getInstance() {
if (instance == null) {
instance = new NotificationManagerUtils();
}
return instance;
}
/**
* 获取通知管理器
* @param context 上下文对象
*/
public void getNotificationService(Context context) {
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
/**
* 获取通知管理器
* @return NotificationManager
*/
public NotificationManager getManager() {
return manager;
}
/**
* 创建通知
* @param context 上下文对象
*/
private void createNotification(Context context) {
if(manager == null){
getNotificationService(context);
}
NotificationCompat.Builder builder = buildNotificaiton(context);
notification = builder.build();
manager.notify(NOTIFICATION_ID, notification);
}
/**
* 发送通知
* @param context 上下文对象
*/
public void sendNotification(Context context){
if(notification == null){
createNotification(context);
}
manager.notify(NOTIFICATION_ID, notification);
}
/**
* 取消通知
*/
public void cancelNotification() {
manager.cancel(NOTIFICATION_ID);
}
/**
* 创建通知builder
* @param context 上下文对象
* @return NotificationCompat.Builder
*/
private NotificationCompat.Builder buildNotificaiton(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setColor(context.getResources().getColor(R.color.colorAccent))
.setContentIntent(createContentIntent(context))
.setContentTitle("通知content")
.setContentText("通知内容")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setBadgeIconType(BADGE_ICON_SMALL)
.setWhen(System.currentTimeMillis())
.setVibrate(new long[]{1000, 500, 2000});
return builder;
}
/**
* 创建通知channel
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel() {
if (manager.getNotificationChannel(CHANNEL_ID) == null) {
CharSequence name = "media Session";
String description = "description";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(
new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
manager.createNotificationChannel(channel);
Log.d(TAG, "createChannel: New channel created");
} else {
Log.d(TAG, "createChannel:Existing channel reused");
}
}
private PendingIntent createContentIntent(Context context) {
Intent openUI = new Intent(context, aClass);
openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(
context, 0, openUI, 0);
}
}