Android 最简单实用的打开手机通知权限设置

您所在的位置:网站首页 小米通知优先级怎么关闭 Android 最简单实用的打开手机通知权限设置

Android 最简单实用的打开手机通知权限设置

2024-07-17 09:17:02| 来源: 网络整理| 查看: 265

最近做离线的消息推送,发现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、一加等多款机型都可以正常跳转到本应用的通知设置界面,没有那么多版本判断和机型适配代码,去掉繁琐的使用,如果大家觉得有用可以自己去试试,亲自体验一下,毕竟说再多再好没有实战数据说明来得真实。因为源码中有判断版本,明天会放出运行效果,好了,今天先到这里,本菜鸡准备休息了~~

关键系统源码如下:



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭