2

How can I check if push notifications are allowed or not?

Android versions below 13 do not require permission to show push notifications. However, the user can disable push notifications in the settings.

Is there a method that will return the correct result of push notifications permission status for any version of android?

P.S. I'm using unity mobile notification package

I try check if notifications are enabled use:

bool result = Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS");

but result is always == false;

Build target Api = 33, but it was tested on android 10.

Thanks

  • Addition: as a result of searching for an answer, I came to the conclusion that it might be worth using an analog of 'NotificationManagerCompat.from(context).areNotificationsEnabled()' as written here **https://stackoverflow.com/questions/27815782/how-do-i-check-whether-my-app-is-allowed-to-post-notifications** But how to implement this for Unity? – Vladimir Simonov Apr 26 '23 at 13:14
  • check my answer :) – Oli Jun 13 '23 at 14:37

2 Answers2

0

Here is a good description of how to check if notifications are enabled for Android versions under Android 13.

You need a custom .aar plugin with this code snippet:

import androidx.core.app.NotificationManagerCompat;
 
public class NotificationStatusChecker {
 
    private static final String LOGTAG = "UnityU";
 
    private Context context;
 
    public NotificationStatusChecker(Context context) {
 
        this.context = context;
    }
 
    public boolean areNotificationsEnabled() {
        boolean result = NotificationManagerCompat.from(this.context).areNotificationsEnabled();
        Log.i(LOGTAG, "Notifications enabled :: " + result);
        return result;
    }
 
    public static boolean areNotificationsEnabledFromContext(Context customContext) {
        boolean result = NotificationManagerCompat.from(customContext).areNotificationsEnabled();
        Log.i(LOGTAG, "From custom context notifications enabled :: " + result);
        return result;
    }
}

and on unity side this c# code:

using UnityEngine;
 
public static class AndroidNotificationStatus
{
#if UNITY_ANDROID
    private const string NOTIFICATION_STATUS_CLASS = "com.XXX.utils.NotificationStatusChecker";
    private const string UNITY_PLAYER_CLASS = "com.unity3d.player.UnityPlayer";
 
    private static AndroidJavaObject _utilsClass;
 
    private static AndroidJavaObject UtilsClass
    {
        get
        {
            if (_utilsClass == null)
            {
                AndroidJavaClass unityPlayer = new AndroidJavaClass(UNITY_PLAYER_CLASS);
                AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                _utilsClass = new AndroidJavaObject(NOTIFICATION_STATUS_CLASS, activity);
            }
 
            return _utilsClass;
        }
    }
 
    private static bool AreNotificationsEnabledInternal()
    {
        var result = UtilsClass.Call<bool>("areNotificationsEnabled");
        return result;
    }
 
    private static bool AreNotificationsEnabledSelfDisposable()
    {
        // self disposable method
        using (var javaUnityPlayer = new AndroidJavaClass(UNITY_PLAYER_CLASS))
        {
            using (var currentActivity = javaUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                using (var utils = new AndroidJavaObject(NOTIFICATION_STATUS_CLASS, currentActivity))
                {
                    return utils.Call<bool>("areNotificationsEnabled");
                }
            }
        }
    }
#endif
}
Oli
  • 1,407
  • 3
  • 30
  • 47
0

You do not need to create jar/aar to get one native bool parameter, you can do everything directly from C# code:

public static class PushNotificationAvailability
{
    public static bool ArePushNotificationsAvailable()
    {
        using var contextClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        using var context = contextClass.GetStatic<AndroidJavaObject>("currentActivity");
        using var notificationManagerClass = new AndroidJavaClass("androidx.core.app.NotificationManagerCompat");
        using var managerInstance = notificationManagerClass.CallStatic<AndroidJavaObject>("from", context.Call<AndroidJavaObject>("getApplicationContext"));
        
        return managerInstance.Call<bool>("areNotificationsEnabled");
    }
}