firebase 如何在Android 13中从Unity请求通知权限

cczfrluj  于 2023-11-21  发布在  Android
关注(0)|答案(2)|浏览(258)

我已经将firebase推送通知服务(云消息)集成到我的Unity项目中。在Android中一切正常,但特别是在Android 13中不工作。
从我的研究中,我发现由于安全原因,权限在Android 13中设置为false,我们必须明确要求用户提供权限。
我还试图检查firebase文档上的推送通知为团结,但我没有发现任何东西。
现在我的问题是如何从Unity c#脚本中请求用户的通知权限?

using Firebase.Messaging;

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

字符串
我也试过这个代码,但它也没有工作,请帮助我

i1icjdpr

i1icjdpr1#

在我们的项目中,我们使用Permission.RequestUserPermission方法。我们要求android.permission.POST_NOTIFICATIONS权限。在此之后,我们创建通知通道。
这是一个代码示例:

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;
...

字符串

b09cbbtk

b09cbbtk2#

最后我用这段代码做到了:

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");
 }

字符串
就像这样,我可以要求任何android许可:-)

相关问题