Android: Check notification permissions and jump to notification settings interface

Keywords: Android

Recently, in order to improve the push function in the project, we need to check whether the push permission is opened when we enter APP. If we do not open the bullet window reminder, when the user clicks on the bullet window, we can jump directly to the notification settings interface of APP.

1. Detecting whether to open notification permission

AreNotifications Enabled () in Notification Manager Compat can be used to determine whether to open notification permissions.

2. Jump to Notification Settings Interface

Assuming that no notification permission is opened, you need to jump to the notification settings interface of APP after clicking. The corresponding Action is Settings.ACTION_APP_NOTIFICATION_SETTINGS, which is added after API 26.

I put this program in MainActivity.class, check the right of notification on the landing homepage, or pop up the warning box to set the notification settings interface that jumps to the software.

 private void notification() {

        NotificationManagerCompat notification = NotificationManagerCompat.from(this);
        boolean isEnabled = notification.areNotificationsEnabled();

        if (!isEnabled) {

            //Unopened Notice
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Tips")
                    .setMessage("Please open notification permission in Notification")
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .setPositiveButton("To set up", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            Intent intent = new Intent();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                intent.putExtra("android.provider.extra.APP_PACKAGE", MainActivity.this.getPackageName());

                            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  //5.0
                                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 (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {  //4.4
                                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                intent.addCategory(Intent.CATEGORY_DEFAULT);
                                intent.setData(Uri.parse("package:" + MainActivity.this.getPackageName()));

                            } else if (Build.VERSION.SDK_INT >= 15) {
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                intent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
                            }
                            startActivity(intent);
                        }
                    })
                    .create();
            alertDialog.show();
            alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLACK);

        }
    }

 

Posted by gm04030276 on Tue, 01 Oct 2019 03:21:38 -0700