NotificationSetUtilDemo【判断APP通知栏权限是否开启,以及如何跳转到应用程序设置界面】
前言
当APP有推送功能时,需要判断当前app在手机中是否开启了允许消息推送,否则即使添加了推送代码仍然收不到通知。
效果图
oppo上的效果:

使用步骤
一、项目组织结构图

注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
1、将NotificationSetUtil.java类复制到项目中
package com.why.project.notificationsetutildemo.utils; import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationManagerCompat; import java.lang.reflect.Field;
import java.lang.reflect.Method; /**
* Created by HaiyuKing
* Used 判断是否开启消息通知,没有开启的话跳转到手机系统设置界面
* 参考:https://blog.csdn.net/lidayingyy/article/details/81778894
* https://www.jianshu.com/p/205bc87cac29
* https://blog.csdn.net/yrmao9893/article/details/74607402/
* https://www.meiwen.com.cn/subject/qcklpftx.html
*/
public class NotificationSetUtil { //判断是否需要打开设置界面
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void OpenNotificationSetting(Context context, OnNextLitener mOnNextLitener) {
if (!isNotificationEnabled(context)) {
gotoSet(context);
} else {
if (mOnNextLitener != null) {
mOnNextLitener.onNext();
}
}
} //判断该app是否打开了通知
/**
* 可以通过NotificationManagerCompat 中的 areNotificationsEnabled()来判断是否开启通知权限。NotificationManagerCompat 在 android.support.v4.app包中,是API 22.1.0 中加入的。而 areNotificationsEnabled()则是在 API 24.1.0之后加入的。
* areNotificationsEnabled 只对 API 19 及以上版本有效,低于API 19 会一直返回true
* */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean isNotificationEnabled(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();
return areNotificationsEnabled;
} String CHECK_OP_NO_THROW = "checkOpNoThrow";
String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION"; 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 = (Integer) opPostNotificationValue.get(Integer.class);
return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED); } catch (Exception e) {
e.printStackTrace();
}
return false; } //打开手机设置页面
/**
* 假设没有开启通知权限,点击之后就需要跳转到 APP的通知设置界面,对应的Action是:Settings.ACTION_APP_NOTIFICATION_SETTINGS, 这个Action是 API 26 后增加的
* 如果在部分手机中无法精确的跳转到 APP对应的通知设置界面,那么我们就考虑直接跳转到 APP信息界面,对应的Action是:Settings.ACTION_APPLICATION_DETAILS_SETTINGS*/
private static void gotoSet(Context context) { Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= 26) {
// android 8.0引导
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
} else if (Build.VERSION.SDK_INT >= 21) {
// android 5.0-7.0
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
} else {
// 其他
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} /*=====================添加Listener回调================================*/
public interface OnNextLitener {
/**
* 不需要设置通知的下一步
*/
void onNext();
} private OnNextLitener mOnNextLitener; public void setOnNextLitener(OnNextLitener mOnNextLitener) {
this.mOnNextLitener = mOnNextLitener;
}
}
NotificationSetUtil.java
三、使用方法
package com.why.project.notificationsetutildemo; import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast; import com.why.project.notificationsetutildemo.utils.NotificationSetUtil; public class MainActivity extends AppCompatActivity { private Context mContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mContext = this; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//判断是否需要开启通知栏功能
NotificationSetUtil.OpenNotificationSetting(mContext, new NotificationSetUtil.OnNextLitener() {
@Override
public void onNext() {
Toast.makeText(mContext,"已开启通知权限",Toast.LENGTH_SHORT).show();
}
});
}
}
}
混淆配置
无
参考资料
Android 判断应用程序获取通知栏权限是否开启,以及如何跳转到应用程序设置界面
项目demo下载地址
暂不需要
NotificationSetUtilDemo【判断APP通知栏权限是否开启,以及如何跳转到应用程序设置界面】的更多相关文章
- iOS用户是否打开APP通知开关跳转到系统的设置界面
1.检测用户是否打开推送通知 /** 系统通知是否打开 @return 是否打开 */ //检测通知是否打开iOS8以后有所变化 所以需要适配iOS7 + (BOOL)openThePushNoti ...
- (转载)安卓6.0之前的系统 判断app是否有录音权限
卓6.0之前的系统 判断app是否有录音权限 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- iOS 权限判断 跳转对应设置界面
相机权限 1.1 使用说明 在合适的地方导入#import <AVFoundation/AVFoundation.h> 使用AVAuthorizationStatus类获取当前权限状态 在 ...
- Android ROM开发(三)——精简官方ROM并且内置ROOT权限,开启Romer之路
Android ROM开发(三)--精简官方ROM并且内置ROOT权限,开启Romer之路 相信ROM的相关信息大家通过前几篇的学习都是有所了解了,这里就不在一一提示了,这里我们下载一个官方包,我们还 ...
- 4.功能三:实现URL地址栏控制(15分) (1)获取到当前访问的控制器和方法(5分) (2)对当前访问的控制器和方法进行判断,有权限继续访问(5分) (3)无权限给出提示(5分)
<?php namespace app\admin\controller; use think\Controller; use think\Request; class Base extends ...
- java中 用telnet 判断服务器远程端口是否开启
package net.jweb.common.util; import java.io.BufferedReader; import java.io.BufferedWriter; import j ...
- 判断App运行是否在前台
转自:http://notes.stay4it.com/2016/02/26/check-if-app-is-running-forground/ 在一些场景中,经常会需要判断App是否在后台运行,比 ...
- 判断App是否在后台运行
在一些场景中,经常会需要判断App是否在后台运行,比如是否显示解锁界面,收到新消息是否显示Notification等.需求可能是多样化的,但所依仗的原理是相通的,今天Stay打算说说这些需求的最优解. ...
- Android判断App是否在前台运行(转)
原文地址: http://blog.csdn.net/zuolongsnail/article/details/8168689 Android开发中,有时候需要判断App是否在前台运行. 代码实现如下 ...
随机推荐
- QM
答案: C 解题: 1. PV = 1,2 / 11% = 10.91 NPV = PV(inflow)-PV(outflow) = 10.91 - 8 = 2.91 2. IRR : NPV = 0 ...
- QM1_Time value of Money
总体框架 Time Value Interest Rate rf: 无风险收益率 (CFA中一般认为是美国短期国债T-bill的收益率) Nominal risk-free rate: 名义无风险税 ...
- 【Unity游戏开发】AssetBundle杂记--AssetBundle的二三事
一.简介 马三在公司大部分时间做的都是游戏业务逻辑和编辑器工具等相关工作,因此对Unity AssetBundle这块的知识点并不是很熟悉,自己也是有打算想了解并熟悉一下AssetBundle,掌握一 ...
- a标签跳转之前加点击事件
①在html标签中出现提示 <a href="http://www.baidu.com" onclick="if(confirm('确认百度吗?')==false) ...
- 服务网关基于RPC的用法
企业为了保护内部系统的安全性,内网与外网都是隔离的,企业的服务应用都是运行在内网环境中,为了安全的考量,一般都不允许外部直接访问.API网关部署在防火墙外面,起到一层挡板作用,内部系统只接受API网关 ...
- Drrols规则引擎
1.什么是规则引擎? 规则引擎是一种嵌套在应用程序中的组件,它实现了将业务规则从应用程序代码中分离出来.规则引擎使用特定的语法编写业务规则,规则引擎可以接受数据输入.解释业务规则.并根据业务规则做出相 ...
- TensorFlow从1到2(八)过拟合和欠拟合的优化
<从锅炉工到AI专家(6)>一文中,我们把神经网络模型降维,简单的在二维空间中介绍了过拟合和欠拟合的现象和解决方法.但是因为条件所限,在该文中我们只介绍了理论,并没有实际观察现象和应对. ...
- ASP.Net Core MVC 发生二次请求
Bug回忆录 昨天搭建新框架的时候,遇到一个很奇怪的“Bug”,每次请求都会触发两次Aciton,举例子吧,Demo: _Layout.cshtml <!DOCTYPE html> < ...
- 入门级 JAVA反射机制
1.什么是反射? Java中的反射机制是Java语言的一个很重要的特性,是Java “动态性” 的重要体现.Java反射机制让我们在程序运行状态中,对于任意一个类,都能知道这个类的所有属性和方法:对于 ...
- TensorFlow从1到2(二)续讲从锅炉工到AI专家
图片样本可视化 原文第四篇中,我们介绍了官方的入门案例MNIST,功能是识别手写的数字0-9.这是一个非常基础的TensorFlow应用,地位相当于通常语言学习的"Hello World!& ...