Request permission on Android 13
This commit is contained in:
Родитель
437c5b2a3c
Коммит
0b7b3b5127
|
@ -2,6 +2,8 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using Unity.Notifications.Android;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Android;
|
||||
|
||||
namespace NotificationSamples.Android
|
||||
{
|
||||
|
@ -30,8 +32,33 @@ namespace NotificationSamples.Android
|
|||
|
||||
public IEnumerator RequestNotificationPermission()
|
||||
{
|
||||
var request = new PermissionRequest();
|
||||
while (request.Status == PermissionStatus.RequestPending)
|
||||
const string notificationPermission = "android.permission.POST_NOTIFICATIONS";
|
||||
const int permissionRequiredSDK = 33;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче