Request permission on Android 13

This commit is contained in:
Aurimas Cernius 2022-09-26 12:16:28 +03:00
Родитель 437c5b2a3c
Коммит 0b7b3b5127
1 изменённых файлов: 30 добавлений и 3 удалений

Просмотреть файл

@ -2,6 +2,8 @@
using System; using System;
using System.Collections; using System.Collections;
using Unity.Notifications.Android; using Unity.Notifications.Android;
using UnityEngine;
using UnityEngine.Android;
namespace NotificationSamples.Android namespace NotificationSamples.Android
{ {
@ -30,9 +32,34 @@ namespace NotificationSamples.Android
public IEnumerator RequestNotificationPermission() public IEnumerator RequestNotificationPermission()
{ {
var request = new PermissionRequest(); const string notificationPermission = "android.permission.POST_NOTIFICATIONS";
while (request.Status == PermissionStatus.RequestPending) const int permissionRequiredSDK = 33;
yield return null;
using (var version = new AndroidJavaClass("android/os/Build$VERSION"))
{
var deviceSdk = version.GetStatic<int>("SDK_INT");
if (deviceSdk < permissionRequiredSDK)
yield break;
}
if (Permission.HasUserAuthorizedPermission(notificationPermission))
yield break;
using (var playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (var activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity"))
{
using (var appInfo = activity.Call<AndroidJavaObject>("getApplicationInfo"))
{
var targetSdk = appInfo.Get<int>("targetSdkVersion");
if (targetSdk < permissionRequiredSDK)
yield break;
}
}
}
Permission.RequestUserPermission(notificationPermission);
yield return null;
} }
/// <inheritdoc /> /// <inheritdoc />