Request permission on Android 13
This commit is contained in:
Родитель
437c5b2a3c
Коммит
0b7b3b5127
|
@ -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 />
|
||||||
|
|
Загрузка…
Ссылка в новой задаче