fix: 更新 PushToken 获取逻辑, 适配新接口

--bug=1030040 --user=yufei.hu 【Always】新老用户都收不到PUSH https://www.tapd.cn/35322409/s/1169863

Signed-off-by: huyufei <yufei.hu@castbox.fm>
胡宇飞 2024-09-04 16:31:58 +08:00
parent 5c2a93f362
commit cb6bc4e794
1 changed files with 53 additions and 4 deletions

View File

@ -1,5 +1,8 @@
using System.Threading.Tasks;
using Firebase.Extensions;
namespace Guru
{
using System;
@ -34,27 +37,72 @@ namespace Guru
FirebaseMessaging.TokenReceived += OnTokenReceived;
FirebaseMessaging.MessageReceived += OnMessageReceived;
GetFCMToken();
DelayGetFCMToken(0);
}
private static void GetFCMToken()
/// <summary>
/// 异步获取 FCM Token
/// </summary>
private static void GetFCMTokenAsync()
{
Debug.Log($"[Firebase] --- Start GetTokenAsync ---");
FirebaseMessaging.GetTokenAsync().ContinueWithOnMainThread(task =>
{
if (!task.IsCompletedSuccessfully)
{
DelayGetFCMToken(10);
return;
}
var token = task.Result;
if (string.IsNullOrEmpty(token))
{
DelayGetFCMToken(10);
return;
}
Debug.Log($"[Firebase] --- GetPushToken:{token}");
IPMConfig.IPM_PUSH_TOKEN = token;
IPMConfig.IS_UPLOAD_DEVICE_SUCCESS = true;
UploadDeviceInfo();
});
}
private static async void DelayGetFCMToken(int seconds = 10)
{
await Task.Delay(seconds * 1000); // 等待 10s
GetFCMTokenAsync();
}
//-------- Upload DeviceInfo -----------
/*
private static void GetFCMToken()
{
CoroutineHelper.Instance.StartCoroutine(CoroutineGetFCMToken());
}
private static IEnumerator CoroutineGetFCMToken()
{
Debug.Log($"[Firebase] --- Start GetTokenAsync ---");
var task = FirebaseMessaging.GetTokenAsync();
while (!task.IsCompleted)
yield return new WaitForEndOfFrame();
if (task.IsFaulted || task.IsCanceled)
{
Log.I(LOG_TAG, $"--- GetTokenAsync Token Failed! {task.Status}");
CoroutineHelper.Instance.StartDelayed(10, GetFCMToken);
}
else
{
Log.I(LOG_TAG, "GetTokenAsync Token: " + task.Result);
Log.I(LOG_TAG, "--- GetTokenAsync Token: " + task.Result);
if (IPMConfig.IPM_PUSH_TOKEN != task.Result || !IPMConfig.IS_UPLOAD_DEVICE_SUCCESS)
{
IPMConfig.IPM_PUSH_TOKEN = task.Result;
@ -62,6 +110,7 @@ namespace Guru
}
}
}
*/
private static void UploadDeviceInfo()
{
@ -73,7 +122,7 @@ namespace Guru
}
else
{
Log.I(LOG_TAG, "FirebaseMessage Send UploadDeviceInfo");
Debug.Log($"[Firebase] --- UploadDeviceInfo ---");
//延时重试
new DeviceInfoUploadRequest()
.SetRetryTimes(1)