當APP有推送功能時,須要判斷當前app在手機中是否開啓了容許消息推送,不然即便添加了推送代碼仍然收不到通知。html
oppo上的效果:java
注意事項:android
一、 導入類文件後須要change包名以及從新import R文件路徑api
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋app
一、將NotificationSetUtil.java類複製到項目中ide
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; } }
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(); } }); } } }
無ui
Android 判斷應用程序獲取通知欄權限是否開啓,以及如何跳轉到應用程序設置界面.net
暫不須要