update: 优化 Push 推送请求
Signed-off-by: huyufei <yufei.hu@castbox.fm>
parent
4a77e9afb9
commit
7237dbfcae
|
|
@ -17,6 +17,7 @@ namespace Guru
|
|||
// public static bool? IsInitMessage;
|
||||
private static bool _isAutoFetchFcmToken = true;
|
||||
private static bool _isFetchOnce = false;
|
||||
// private static bool _isOnFetching = false;
|
||||
|
||||
public static void SetAutoFetchFcmToken(bool value)
|
||||
{
|
||||
|
|
@ -25,6 +26,13 @@ namespace Guru
|
|||
|
||||
private static void InitializeMessage()
|
||||
{
|
||||
// 初始化回调挂载
|
||||
FirebaseMessaging.TokenReceived -= OnTokenReceived;
|
||||
FirebaseMessaging.MessageReceived -= OnMessageReceived;
|
||||
|
||||
FirebaseMessaging.TokenReceived += OnTokenReceived;
|
||||
FirebaseMessaging.MessageReceived += OnMessageReceived;
|
||||
|
||||
if (_isAutoFetchFcmToken)
|
||||
{
|
||||
StartFetchFcmToken();
|
||||
|
|
@ -36,8 +44,6 @@ namespace Guru
|
|||
if (_isFetchOnce) return;
|
||||
_isFetchOnce = true;
|
||||
|
||||
FirebaseMessaging.TokenReceived += OnTokenReceived;
|
||||
FirebaseMessaging.MessageReceived += OnMessageReceived;
|
||||
DelayGetFCMToken(0);
|
||||
}
|
||||
|
||||
|
|
@ -46,11 +52,12 @@ namespace Guru
|
|||
/// </summary>
|
||||
private static void GetFCMTokenAsync()
|
||||
{
|
||||
Debug.Log($"[SDK][Firebase] --- Start GetTokenAsync ---");
|
||||
Debug.Log($"{LOG_TAG}[SDK]--- Start GetTokenAsync ---");
|
||||
FirebaseMessaging.GetTokenAsync().ContinueWithOnMainThread(task =>
|
||||
{
|
||||
if (!task.IsCompletedSuccessfully)
|
||||
if (task.IsCanceled || task.IsFaulted)
|
||||
{
|
||||
// task 获取失败
|
||||
DelayGetFCMToken(_retryTokenDelay);
|
||||
return;
|
||||
}
|
||||
|
|
@ -59,12 +66,13 @@ namespace Guru
|
|||
if (string.IsNullOrEmpty(token)
|
||||
&& string.IsNullOrEmpty(IPMConfig.IPM_PUSH_TOKEN))
|
||||
{
|
||||
// 拉取到了空值, 且无缓存值
|
||||
DelayGetFCMToken(_retryTokenDelay);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Debug.Log($"[Firebase] --- GetPushToken:{token}");
|
||||
Debug.Log($"{LOG_TAG}[SDK] --- GetPushToken:{token}");
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
IPMConfig.IPM_PUSH_TOKEN = token;
|
||||
|
|
@ -73,7 +81,7 @@ namespace Guru
|
|||
});
|
||||
}
|
||||
|
||||
private static async void DelayGetFCMToken(int seconds = 10)
|
||||
private static async void DelayGetFCMToken(int seconds = 2)
|
||||
{
|
||||
await Task.Delay(seconds * 1000); // 等待 10s
|
||||
GetFCMTokenAsync();
|
||||
|
|
@ -134,7 +142,7 @@ namespace Guru
|
|||
.SetFailCallBack(() =>
|
||||
{
|
||||
Debug.Log($"{LOG_TAG} --- UploadDeviceInfo:failed");
|
||||
double retryDelay = Math.Pow(2, _retryDeviceCount);
|
||||
double retryDelay = Math.Pow(2, Mathf.Min(_retryDeviceCount, 6));
|
||||
_retryDeviceCount++;
|
||||
CoroutineHelper.Instance.StartDelayed((float) retryDelay, UploadDeviceInfo);
|
||||
}).Send();
|
||||
|
|
|
|||
Loading…
Reference in New Issue