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