Push Notifications Android : reading settings 'blocked' by user

Our app has implemented push notifications.

I want to show in our settings menu, whether push notifications are enabled. The user can then click on the button and open the settings app to change that setting.

On iOS this works fine by using “checkPermissions” . If the user has switched off (disabled) push notifications, it returns “denied” and we show “Notifications are disabled”

On Android, PushNotifications.checkPermissions() always returns ‘granted’, even when the user has disabled the push notifications using the settings app.

Is there a way to read the settings from Android, so we can show the correct text?

As far as i know you can not disable the Push Notifications on Android, that’s why it always returns true.

What you can disable is the presenting of the Push Notifications, but i don’t know if you can read that status. I would recommend to research about that nativly and if it works native than it can by a plugin too

Yes, that is what I am talking about, disable presenting them.

I just found the plugin Diagnostics. Diagnostic - Ionic Documentation

It describes this:

isRemoteNotificationsEnabled()

Platforms: Android and iOS

Checks if remote (push) notifications are enabled.

On Android, returns whether notifications for the app are not blocked.

Returns true if app is registered for remote notifications AND “Allow Notifications” switch is ON AND alert style is not set to “None” (i.e. “Banners” or “Alerts”).

Going to test it now and will report back if I run into any issues (I would prefer not to load all the diagnostics, because I don’t need the location diagnostics etc).

1 Like

The Diagnostics Plugin has a great mechanism to only load the modules that you need, But this only works for Cordova. But if the method work you can create your own small Plugin right inside your App, that just does that one thing

1 Like

The diagnostics plugin works fine in the app. However because it is overkill for our needs, I have taken an alternative route: I have created a small Plugin in our App, as suggested by @EinfachHans

package nl.doemeebeslismee.doemee;

import com.getcapacitor.Plugin;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.PluginCall;
import com.getcapacitor.JSObject;
import com.getcapacitor.annotation.CapacitorPlugin;

import androidx.core.app.NotificationManagerCompat;

@CapacitorPlugin(name = "LocationEnabled")
public class LocationEnabledPlugin extends Plugin{

    @PluginMethod()
    public void getLocationSettings(PluginCall call){
        JSObject locationSettings = new JSObject();
        try{
            NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getContext());
            boolean result = notificationManagerCompat.areNotificationsEnabled();
            locationSettings.put("isEnabled", result);
            call.resolve(locationSettings);
        }
        catch(Exception e){
            call.reject(e.getLocalizedMessage());
        }

    }

}

And added it to the MainActivity.java

package nl.doemeebeslismee.doemee;

import android.os.Bundle;

import com.getcapacitor.BridgeActivity;
import nl.doemeebeslismee.doemee.LocationEnabledPlugin;


public class MainActivity extends BridgeActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerPlugin(LocationEnabledPlugin.class);
    }
}

Then I created my typescript file:

import { registerPlugin } from '@capacitor/core';

export interface LocationEnabledPlugin{
    getLocationSettings(): Promise<{ isEnabled: boolean }>;
   
}
    
const LocationEnabled = registerPlugin<LocationEnabledPlugin>('LocationEnabled');
    
export default LocationEnabled;

And you are ready to use it:

 if (isPlatform("android") && Capacitor.isPluginAvailable("PushNotifications")) {

                const remoteNotifications = await LocationEnabled.getLocationSettings();
                if(remoteNotifications.isEnabled){
                    setSettingsText("Puhs notifications are enabled" );
                }
                else {
                    setSettingsText("Enable push notifications");
                }
            }
1 Like

local-notifications plugin has an areEnabled method you can use

2 Likes

Thanks, but I am using remote notifications. It would be a nice addition to that plugin to have the same functionality as the local-notifications plugin though.

The setting it’s still the same, doesn’t matter if it’s push or local, if the user disabled the notifications none will work and that function will tell you if they are enabled or not

I get that, but it means I have to install a new plugin just to get the settings on Android… it would be nice to have it in the other plugin, where I need it.