parent
a53c153338
commit
eba48e4a75
|
|
@ -18,6 +18,13 @@ namespace Guru.Notification
|
||||||
|
|
||||||
private bool _initOnce = false;
|
private bool _initOnce = false;
|
||||||
private string _notiStatus;
|
private string _notiStatus;
|
||||||
|
private bool _isPluginsInited = false;
|
||||||
|
|
||||||
|
private string SavedNotiPermStatus
|
||||||
|
{
|
||||||
|
get => PlayerPrefs.GetString(nameof(SavedNotiPermStatus), "");
|
||||||
|
set => PlayerPrefs.SetString(nameof(SavedNotiPermStatus), value);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化
|
/// 初始化
|
||||||
|
|
@ -25,6 +32,14 @@ namespace Guru.Notification
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
if (!_initOnce) return;
|
if (!_initOnce) return;
|
||||||
|
_initOnce = true;
|
||||||
|
|
||||||
|
_notiStatus = STATUS_DENIDED;
|
||||||
|
if (!string.IsNullOrEmpty(SavedNotiPermStatus))
|
||||||
|
{
|
||||||
|
_notiStatus = SavedNotiPermStatus;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#if UNITY_ANDROID
|
#if UNITY_ANDROID
|
||||||
InitPlugins();
|
InitPlugins();
|
||||||
|
|
@ -47,10 +62,11 @@ namespace Guru.Notification
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置授权状态
|
/// 设置授权状态
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="flag"></param>
|
/// <param name="status">授权状态</param>
|
||||||
private void SetGrantStatus(bool flag = false)
|
private void SetGrantStatus(string status)
|
||||||
{
|
{
|
||||||
_notiStatus = flag ? STATUS_GRANTED : STATUS_DENIDED;
|
_notiStatus = status;
|
||||||
|
SavedNotiPermStatus = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -93,6 +109,8 @@ namespace Guru.Notification
|
||||||
AndroidNotificationCenter.Initialize();
|
AndroidNotificationCenter.Initialize();
|
||||||
Debug.Log($"[Noti][AND] --- Notification Service InitPlugins");
|
Debug.Log($"[Noti][AND] --- Notification Service InitPlugins");
|
||||||
|
|
||||||
|
_isPluginsInited = true;
|
||||||
|
|
||||||
UpdateNotiStatus();
|
UpdateNotiStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,21 +119,26 @@ namespace Guru.Notification
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void UpdateNotiStatus()
|
private void UpdateNotiStatus()
|
||||||
{
|
{
|
||||||
|
if (!_isPluginsInited) return;
|
||||||
|
|
||||||
TryExecute(() =>
|
TryExecute(() =>
|
||||||
{
|
{
|
||||||
_permissionStatus = AndroidNotificationCenter.UserPermissionToPost;
|
_permissionStatus = AndroidNotificationCenter.UserPermissionToPost;
|
||||||
|
var status = "";
|
||||||
switch (_permissionStatus)
|
switch (_permissionStatus)
|
||||||
{
|
{
|
||||||
case PermissionStatus.NotRequested:
|
// case PermissionStatus.NotRequested:
|
||||||
_notiStatus = STATUS_NOT_DETERMINED;
|
// _notiStatus = STATUS_NOT_DETERMINED;
|
||||||
break;
|
// break;
|
||||||
case PermissionStatus.Allowed:
|
case PermissionStatus.Allowed:
|
||||||
_notiStatus = STATUS_DENIDED;
|
status = STATUS_DENIDED;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
_notiStatus = STATUS_DENIDED;
|
status = STATUS_DENIDED;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SetGrantStatus(status);
|
||||||
Debug.LogWarning($"[SDK][AND] --- UpdateNotiStatus:{_notiStatus} | UserPermissionToPost:{_permissionStatus}");
|
Debug.LogWarning($"[SDK][AND] --- UpdateNotiStatus:{_notiStatus} | UserPermissionToPost:{_permissionStatus}");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -125,48 +148,35 @@ namespace Guru.Notification
|
||||||
private PermissionCallbacks _permissionCallbacks;
|
private PermissionCallbacks _permissionCallbacks;
|
||||||
private void RequestAndroidPermission(Action<string> callback = null)
|
private void RequestAndroidPermission(Action<string> callback = null)
|
||||||
{
|
{
|
||||||
_onPermissionCallback = callback;
|
if (!_isPluginsInited)
|
||||||
|
{
|
||||||
|
callback?.Invoke(_notiStatus);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
UpdateNotiStatus();
|
UpdateNotiStatus();
|
||||||
|
|
||||||
|
if (_notiStatus == STATUS_GRANTED)
|
||||||
|
{
|
||||||
|
callback?.Invoke(_notiStatus);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_onPermissionCallback = callback;
|
||||||
TryExecute(() =>
|
TryExecute(() =>
|
||||||
{
|
{
|
||||||
bool hasPerm = Permission.HasUserAuthorizedPermission(PERMISSION_POST_NOTIFICATION);
|
|
||||||
var sdkInt = GetAndroidSDKVersion();
|
var sdkInt = GetAndroidSDKVersion();
|
||||||
if (sdkInt < REQUEST_PERMISSION_SDK_VERSION)
|
if (sdkInt < REQUEST_PERMISSION_SDK_VERSION)
|
||||||
{
|
{
|
||||||
// 低版本处理方式
|
// 低版本处理方式
|
||||||
if (_notiStatus == STATUS_GRANTED)
|
Debug.Log($"[SDK][Noti] --- #2 SDK {sdkInt} not requested -> open channel");
|
||||||
{
|
AndroidNotificationCenter.OpenNotificationSettings(FCM_DEFAULT_CHANNEL_ID); // 打开ChannelID
|
||||||
Debug.Log($"[SDK][Noti] --- #1 SDK {sdkInt} has already Grated!");
|
SetGrantStatus(STATUS_GRANTED);
|
||||||
SetGrantStatus(true);
|
|
||||||
// 已经允许了
|
|
||||||
callback?.Invoke(_notiStatus);
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (_permissionStatus == PermissionStatus.NotRequested ||
|
|
||||||
_permissionStatus == PermissionStatus.RequestPending ||
|
|
||||||
AndroidNotificationCenter.ShouldShowPermissionToPostRationale)
|
|
||||||
{
|
|
||||||
Debug.Log($"[SDK][Noti] --- #2 SDK {sdkInt} not requested -> open channel");
|
|
||||||
AndroidNotificationCenter.OpenNotificationSettings(FCM_DEFAULT_CHANNEL_ID); // 打开ChannelID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (hasPerm)
|
|
||||||
{
|
|
||||||
Debug.Log($"[SDK][Noti] --- #3 SDK {sdkInt} :: Post permission has already granted!");
|
|
||||||
SetGrantStatus(true);
|
|
||||||
// 已经允许了
|
|
||||||
callback?.Invoke(_notiStatus);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 未允许
|
// SDK 33 以上,请求弹窗
|
||||||
// 则请求弹窗
|
Debug.Log($"[SDK][Noti] --- #4 SDK {sdkInt} :: Ask Post Permission");
|
||||||
Debug.Log($"[SDK][Noti] --- #3 SDK {sdkInt} :: Ask Post Permission");
|
|
||||||
Permission.RequestUserPermission(PERMISSION_POST_NOTIFICATION, SetupPermissionCallbacks());
|
Permission.RequestUserPermission(PERMISSION_POST_NOTIFICATION, SetupPermissionCallbacks());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -204,7 +214,7 @@ namespace Guru.Notification
|
||||||
_notiStatus = STATUS_GRANTED;
|
_notiStatus = STATUS_GRANTED;
|
||||||
_onPermissionCallback?.Invoke(_notiStatus);
|
_onPermissionCallback?.Invoke(_notiStatus);
|
||||||
}
|
}
|
||||||
|
SetGrantStatus(STATUS_GRANTED);
|
||||||
DisposePermissionCallbacks();
|
DisposePermissionCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -219,7 +229,7 @@ namespace Guru.Notification
|
||||||
_notiStatus = STATUS_DENIDED;
|
_notiStatus = STATUS_DENIDED;
|
||||||
_onPermissionCallback?.Invoke(_notiStatus);
|
_onPermissionCallback?.Invoke(_notiStatus);
|
||||||
}
|
}
|
||||||
|
SetGrantStatus(STATUS_DENIDED);
|
||||||
DisposePermissionCallbacks();
|
DisposePermissionCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue