Skip to content

Android Notification

谁与争辉hui edited this page May 3, 2017 · 2 revisions

如何提高通知有消息

一,Android 手机通过通知提高日活增加应用的新鲜度是很有帮助的,但是Android手机同时支持开启和关闭通知,但是设置关闭之后就无法接收通知,为了更好的提高日活增加新鲜度,可以判断是否关闭通知,提示用户打开通知。

Notification_enable

Android SDK_INT 在高于Build.VERSION_CODES.KITKAT(19)可以判断通知是否被关闭。如下:

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) {
    try {
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            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;
            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 (Exception e) {

    }
    return true;
}
Clone this wiki locally