package com.example.my; import android.annotation.TargetApi; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.graphics.Color; import android.net.Uri; import android.os.Build; import androidx.core.app.NotificationCompat; import static android.app.Notification.VISIBILITY_SECRET; import static androidx.core.app.NotificationCompat.PRIORITY_DEFAULT; public class NotificationUtils extends ContextWrapper { public static final String CHANNEL_ID = "default"; private static final String CHANNEL_NAME = "Default Channel"; private static final String CHANNEL_DESCRIPTION = "this is default channel!"; private NotificationManager mManager; private Context context; private String EXTRA_EXTRA; public NotificationUtils(Context base, String EXTRA_EXTRA) { super(base); context = base; this.EXTRA_EXTRA = EXTRA_EXTRA; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(); } } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel() { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); //是否绕过请勿打扰模式 channel.canBypassDnd(); //闪光灯 channel.enableLights(true); //锁屏显示通知 channel.setLockscreenVisibility(VISIBILITY_SECRET); //闪关灯的灯光颜色 channel.setLightColor(Color.RED); //桌面launcher的消息角标 channel.canShowBadge(); //是否允许震动 channel.enableVibration(true); //获取系统通知响铃声音的配置 channel.getAudioAttributes(); //获取通知取到组 channel.getGroup(); //设置可绕过 请勿打扰模式 channel.setBypassDnd(true); //设置震动模式 channel.setVibrationPattern(new long[]{100, 100, 200}); //是否会有灯光 channel.shouldShowLights(); getManager().createNotificationChannel(channel); } private NotificationManager getManager() { if (mManager == null) { mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } return mManager; } /** * 发送通知 */ public void sendNotification(String title, String content) { NotificationCompat.Builder builder = getNotification(title, content); getManager().notify(1, builder.build()); } private NotificationCompat.Builder getNotification(String title, String content) { NotificationCompat.Builder builder = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID); } else { builder = new NotificationCompat.Builder(getApplicationContext()); builder.setPriority(PRIORITY_DEFAULT); } //标题 builder.setContentTitle(title); //文本内容 builder.setContentText(content); //小图标 builder.setSmallIcon(R.mipmap.ic_launcher); //设置点击信息后自动清除通知 builder.setAutoCancel(true); //点击通知之后要发送的广播 // int id = (int) (System.currentTimeMillis() / 1000); // clickIntent.addCategory(context.getPackageName()); // clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED); // clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, EXTRA_EXTRA); Intent mIntent2=new Intent(context, MainActivity.class); mIntent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity( context,0,mIntent2,PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); return builder; } /** * 发送通知 */ public void sendNotification(int notifyId, String title, String content) { NotificationCompat.Builder builder = getNotification(title, content); getManager().notify(notifyId, builder.build()); } }
NotificationUtils兼容9.0
最新推荐文章于 2025-05-23 10:44:44 发布