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

问题描述 投票:0回答:1

我已将 firebase 推送通知服务(云消息传递)集成到我的 Unity 项目中。在 Android 中一切正常,但特别是 在 Android 13 中它不起作用。

根据我的研究,我发现由于安全原因,权限在 android 13 中设置为 false,我们必须明确向用户请求权限。

我还尝试检查有关推送通知的 Firebase 文档以实现统一,但我没有找到任何内容。

现在我的问题是如何从Unity c#脚本向用户请求通知权限?

using Firebase.Messaging;

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

我也尝试过这个代码,但它也不起作用,请帮助我

c# firebase unity-game-engine push-notification game-development
1个回答
0
投票

在我们的项目中,我们使用 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;
...
© www.soinside.com 2019 - 2024. All rights reserved.