Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity mobile notification. Check if notifications are enabled for android

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

like image 616
Vladimir Simonov Avatar asked Dec 09 '25 08:12

Vladimir Simonov


1 Answers

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");
    }
}
like image 117
Rinaldo Avatar answered Dec 12 '25 00:12

Rinaldo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!