others-How to solve android is blocking this app's notification problem on android 13?

1. Purpose

In this post, I will show you how to solve the following error when trying to turn on notification of an android app.
the toggle is greyed out, can not turn it on. How to solve this problem?

At your request, Android is blocking this app's notificationss from appearing on this device.


2. Solution

2.1 How to fix it

Add POST_NOTIFICATIONS permission declaration to AndroidManifest.xml

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

And then check this perm in your onCreate of your main activity:

public static final String POST_NOTIFICATIONS="android.permission.POST_NOTIFICATIONS";  
  
public static void requestNotificationPermission(Activity activity) {  
    requestNotificationPermission(activity,false);  
}  
  
public static void requestNotificationPermission(Activity activity,boolean fromMainUI) {  
    if(fromMainUI) {  
        if (SystemUtils.isNotifPermChecked(activity)) {  
            return;  
        }  
        SystemUtils.setNotifPermChecked(activity, true);  
    }  
    if (Build.VERSION.SDK_INT >= 33) {  
        if (ActivityCompat.checkSelfPermission(activity, POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED) {  
            if (!ActivityCompat.shouldShowRequestPermissionRationale( activity, POST_NOTIFICATIONS)) {  
                enableNotification(activity);  
            }else{  
                ActivityCompat.requestPermissions( activity,new String[]{POST_NOTIFICATIONS},100);  
            }  
        }  
    } else {  
        boolean enabled = NotificationManagerCompat.from(activity).areNotificationsEnabled();  
        if (!enabled) {  
            enableNotification(activity);  
        }  
    }  
}  
  
private static void enableNotification(Context context) {  
    try {  
        Intent intent = new Intent();  
        intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);  
        intent.putExtra(Settings.EXTRA_APP_PACKAGE,context. getPackageName());  
        intent.putExtra(Settings.EXTRA_CHANNEL_ID, context.getApplicationInfo().uid);  
        intent.putExtra("app_package", context.getPackageName());  
        intent.putExtra("app_uid", context.getApplicationInfo().uid);  
        context.startActivity(intent);  
    } catch (Exception e) {  
        e.printStackTrace();  
        Intent intent = new Intent();  
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  
        Uri uri = Uri.fromParts("package",context. getPackageName(), null);  
        intent.setData(uri);  
        context.startActivity(intent);  
    }  
}

and this code to check and make sure the permission is granted to your app by users:

private void checkAndroid13NotifPerm() {  
    if(SystemUtils.getApiLevel(this)>= Build.VERSION_CODES.TIRAMISU) {  
        if (!SystemUtils.isNotifPermChecked(this)) {  
            ConfirmUtil.showConfirmDialogOkCancel(this, R.string.confirm_title,  
                    R.string.need_notif_perm, new ConfirmUtil.IConfirmResponder() {  
                        @Override  
                        public void confirm(int confirmResultCode) {  
                            if (confirmResultCode == ConfirmUtil.IConfirmResponder.CONFIRM_RESULT_OK) {  
                                PostNotifUtils.requestNotificationPermission(MainActivity.this,  
                                        true);  
                            }  
                        }  
                    });  
        }  
    }  
}

@Override  
protected void onCreate(@Nullable Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    
	checkAndroid13NotifPerm();
}

now check again, you can see that it’s working now, you can click the button now

2.2 Why the solution working?

First things first, we need to know what is “POST_NOTIFICATIONS” permission in android:

The POST_NOTIFICATIONS permission is a runtime permission that allows an app to post notifications to the user’s device. This permission was introduced in Android 13.

Prior to Android 13, apps could post notifications without requesting permission. However, this led to a proliferation of spammy and annoying notifications, and users had no way to prevent apps from sending them notifications.

The POST_NOTIFICATIONS permission gives users more control over which apps can send them notifications. When an app requests this permission, the user will be prompted to grant or deny permission. If the user denies permission, the app will not be able to post notifications to the user’s device.

Here are some of the benefits of the POST_NOTIFICATIONS permission:

  • Improved user experience: Users can now choose which apps can send them notifications, which can help to reduce the number of spammy and annoying notifications.
  • Increased security: The POST_NOTIFICATIONS permission helps to protect users from apps that might try to send them malicious notifications.
  • Improved battery life: Apps that do not have the POST_NOTIFICATIONS permission will not be able to post notifications, which can help to improve battery life.

If you are developing an app that needs to post notifications, you should request the POST_NOTIFICATIONS permission. This will help to ensure that your app is respectful of user privacy and that it does not drain the user’s battery.

Here are some best practices for requesting the POST_NOTIFICATIONS permission:

  • Only request the permission when it is necessary. Do not request the permission unless your app absolutely needs to post notifications.
  • Explain to the user why your app needs the permission. When you request the permission, be sure to explain to the user why your app needs it. This will help to build trust with the user and make them more likely to grant permission.
  • Respect the user’s decision. If the user denies permission, do not try to trick or force them into granting permission. Simply respect their decision and move on.

3. Summary

In this post, I demonstrated how to solve the blocking problem on android 13 , the key solution is to make sure that you have the POST_NOTIFICATIONS permission. That’s it, thanks for your reading.