skip to Main Content

I have integrated firebase Push Notification service (cloud Messaging) into my Unity Project. Everything works fine in android, but specifically in android 13 it is not working.

From my research i found that due to security reasons the permission is set to false in android 13 and we have to explicitly ask user for the permission.

I also tried to check the firebase documentation on push notification for unity but i didn’t found anything.

Now my question is how to ask for notification permission from users from Unity c# script?

using Firebase.Messaging;

async void Awake()
{
 await FirebaseMessaging.RequestPermissionAsync();
}

I also tried this code but it also didn’t worked , please help me

2

Answers


  1. Chosen as BEST ANSWER

    I finally managed to do it with this code:

    void askRequst()//call this function to ask request
     {
         if (Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS"))
         {
             print("permission granted!!");
         }
         else
         {
             var callbacks = new PermissionCallbacks();
             callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
             callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
             callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
             Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS", callbacks);
         }
    
     }
     internal void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string permissionName)
     {
         Debug.Log($"{permissionName} PermissionDeniedAndDontAskAgain");
     }
    
     internal void PermissionCallbacks_PermissionGranted(string permissionName)
     {
         Debug.Log($"{permissionName} PermissionCallbacks_PermissionGranted");
     }
    
     internal void PermissionCallbacks_PermissionDenied(string permissionName)
     {
         Debug.Log($"{permissionName} PermissionCallbacks_PermissionDenied");
     }
    

    and just like this, i can ask for any android permission:-)


  2. In our project we use Permission.RequestUserPermission method. We ask for android.permission.POST_NOTIFICATIONS permission. After this we create notification channel.

    This is a code example:

    private static int GetApiLevel()
    {
        using var version = new AndroidJavaClass("android.os.Build$VERSION");
        return version.GetStatic<int>("SDK_INT");
    }
    
    
    ...
    var apiLevel = GetApiLevel();
    Debug.Log($"Android API level = {apiLevel}");
    if (apiLevel > 32)
    {
        // for API level > 32 requesting permissions works well.
        // for API level <= 32 it does nothing and always fails.
        ... Request permission ...
    }
    
    ...
    // Then we create or obtain notification chanell to push notifications.
    var existingChannel = AndroidNotificationCenter.GetNotificationChannel(ChannelId);
    if (existingChannel.Id == ChannelId)
    {
        _channelInitialized = existingChannel.Enabled;
        return;
    }
    
    // Create new channel
    var channel = new AndroidNotificationChannel()
    {
        Id = ChannelId,
        Name = "SomeName",
        Importance = Importance.Default,
        Description = "Generic notifications",
        CanShowBadge = true,
        EnableVibration = true,
        LockScreenVisibility = LockScreenVisibility.Public
    };
    Debug.Log("Notifications channel initialization");
    AndroidNotificationCenter.RegisterNotificationChannel(channel);
    var c = AndroidNotificationCenter.GetNotificationChannel(ChannelId);
    Debug.Log($"Channel enabled = {c.Enabled}");
    _channelInitialized = c.Enabled;
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search