最近做离线的消息推送,发现oppo、vivo、小米等有些手机机型通知权限默认是关闭的,需要手动打开,网上有很多介绍怎么设置和打开的,但是发现都要适配各种版本和各大机型,太麻烦和繁琐,不是我想要的效果,于是自己整理了一个简单方便实用的方法,不多说,直接上代码:
1.封装的方法如下:
/\*\*
\* 打开通知权限
\*
\* @param context
\*/
public static void openNotificationSettingsForApp(Context context) {
// Links to this app's notification settings.
Intent intent = new Intent();
intent.setAction("android.settings.APP\_NOTIFICATION\_SETTINGS");
intent.putExtra("app\_package", context.getPackageName());
intent.putExtra("app\_uid", context.getApplicationInfo().uid);
// for Android 8 and above
intent.putExtra("android.provider.extra.APP\_PACKAGE", context.getPackageName());
context.startActivity(intent);
}
2.在MainActivity使用:
NotificationManagerCompat notification = NotificationManagerCompat.from(mContext);
boolean isEnabled = notification.areNotificationsEnabled();
if (!isEnabled) {
toTipDialog();
}
3.完整的工具类代码如下:
/\*\*
\* @auth: njb
\* @date: 2021/7/28 10:54
\* @desc: 手机通知设置工具类
\*/
public class NotifyManagerUtils {
static long\[\] vibrate = {100, 200, 300, 400, 500, 400, 300, 200, 400};
/\*\*
\* 打开通知权限
\*
\* @param context
\*/
public static void openNotificationSettingsForApp(Context context) {
// Links to this app's notification settings.
Intent intent = new Intent();
intent.setAction("android.settings.APP\_NOTIFICATION\_SETTINGS");
intent.putExtra("app\_package", context.getPackageName());
intent.putExtra("app\_uid", context.getApplicationInfo().uid);
// for Android 8 and above
intent.putExtra("android.provider.extra.APP\_PACKAGE", context.getPackageName());
context.startActivity(intent);
}
public static void notificationActions(Context context,String content,String title) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION\_SERVICE);
RemoteViews remoteViews = new RemoteViews(CommentUtils.getPackageName(context), R.layout.layout\_notify);
remoteViews.setTextViewText(R.id.tv\_title, title);
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG\_ACTIVITY\_NEW\_TASK | Intent.FLAG\_ACTIVITY\_RESET\_TASK\_IF\_NEEDED | Intent.FLAG\_ACTIVITY\_CLEAR\_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG\_UPDATE\_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.app\_icon\_round\_connor);
builder.setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.app\_icon\_round\_connor));
builder.setContentTitle(title);
builder.setContentText(content);
builder.setContent(remoteViews);
builder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
// 用户可以看到的通知渠道的名字.
CharSequence name = "notification channel";
// 用户可以看到的通知渠道的描述
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK\_INT >= android.os.Build.VERSION\_CODES.O) {
mChannel = new NotificationChannel("1", name, NotificationManager.IMPORTANCE\_HIGH);
// 配置通知渠道的属性
mChannel.setDescription(content);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(true);
mChannel.setVibrationPattern(vibrate);
//最后在notificationmanager中创建该通知渠道
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(1, builder.build());
}
}
4.是不是发现很简单有没有,一个方法就搞定,而且是适配了各大厂商机型,目前亲自测试了小米、华为、oppo、Vivo、三星、google、一加等多款机型都可以正常跳转到本应用的通知设置界面,没有那么多版本判断和机型适配代码,去掉繁琐的使用,如果大家觉得有用可以自己去试试,亲自体验一下,毕竟说再多再好没有实战数据说明来得真实。因为源码中有判断版本,明天会放出运行效果,好了,今天先到这里,本菜鸡准备休息了~~
关键系统源码如下: