本文主要是介绍突破 oppo等手机安装软件后通知权限默认关闭的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先说场景,公司在app上集成推送功能,可是在OPPO手机上一直接收不到推送消息,debug一下发现推送消息可以接受到但是没有吊起通知栏。去设置一看通知栏没给权限。又去oppo开放平台咨询,oppo官方回答如下
咋办呢。。。。。。
首先想到是不是和运行时权限一样,用到时申请一下呢,便尝试了以下方法。
方案一:
通过反射检查通知是否打开,没打开便去申请。
package com.example.jpushdemo;import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.provider.Settings;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/***/
public class NotificationsUtils {private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";//检测 通知状态 是否已打开public static boolean isNotificationEnabled(Context context) {AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);ApplicationInfo appInfo = context.getApplicationInfo();String pkg = context.getApplicationContext().getPackageName();int uid = appInfo.uid;Class appOpsClass = null;/* Context.APP_OPS_MANAGER */try {appOpsClass = Class.forName(AppOpsManager.class.getName());Method checkOpNoThrowMethod =appOpsClass.getMethod(CHECK_OP_NO_THROW,Integer.TYPE,Integer.TYPE,String.class);Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);int value = (int) opPostNotificationValue.get(Integer.class);return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==AppOpsManager.MODE_ALLOWED);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (NoSuchFieldException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}return false;}/*** 跳转设置页面 去设置通知权限* @param context*/public static void toNotificationSetting(Context context) {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {Intent intent = new Intent();intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");intent.putExtra("app_package", context.getApplicationContext().getPackageName());intent.putExtra("app_uid", context.getApplicationInfo().uid);context.startActivity(intent);} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT) {Intent intent = new Intent();intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.setData(Uri.parse("package:" + context.getApplicationContext().getPackageName()));context.startActivity(intent);}}
}
这篇关于突破 oppo等手机安装软件后通知权限默认关闭的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!