判斷App通知是否被屏蔽,彈窗提醒並提供跳轉到對應開啓通知頁面的按鈕

需求:

1、全新、升級安裝後判斷系統是否屏蔽App的通知。

2、如果通知被屏蔽,彈出提醒開啓通知的彈窗。

3、點擊彈窗中“立即開啓”按鈕,跳轉到系統對應的此App的通知設置界面。

解決方法:

1、判斷通知App的通知功能是否被屏蔽。代碼如下:

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
            boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();

備註:此方法只針對Android4.3以及以上

2、跳轉到對應的通知設置頁面。代碼如下:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                        Intent intent = new Intent();
                        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                        intent.putExtra("app_package", MainActivity.this.getPackageName());
                        intent.putExtra("app_uid", MainActivity.this.getApplicationInfo().uid);
                        startActivity(intent);
                    } else if (android.os.Build.VERSION.SDK_INT == 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:" + MainActivity.this.getPackageName()));
                        startActivity(intent);
                    } else {
                        Intent localIntent = new Intent();
                        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                        localIntent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
                        startActivity(localIntent);
                    }

備註:

判斷跳轉的代碼當中【Build.VERSION.SDK_INT < Build.VERSION_CODES.O】是因爲在系統8.0以上,由於權限問題導致不能成功跳轉到設置界面。解決方法尚待探究。如果有解決的朋友歡迎留言指導。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章