fix: 处理厂商平台token一定时间内不会重复获取问题

This commit is contained in:
Liuyf 2025-02-25 15:52:14 +08:00
parent 5aa6958acc
commit 1a61d080f7
2 changed files with 27 additions and 17 deletions

View File

@ -28,8 +28,8 @@ class XSJPushProvider {
9: 'jiguang'
};
final JPush jpush = JPush();
late Completer<Map<String, dynamic>> _jpushRegistrationIdCompleter;
late Completer<Map<String, dynamic>> _vendorTokenCompleter;
Completer<Map<String, dynamic>>? _jpushRegistrationIdCompleter;
Completer<Map<String, dynamic>>? _vendorTokenCompleter;
Future<void> resetJPushService() async {
debugPrint("resetJPushService start");
@ -86,39 +86,42 @@ class XSJPushProvider {
case CMD_GET_REGISTRATION_ID:
final bool isNullOrBlank =
GetUtils.isNullOrBlank(data['message']) ?? true;
if (!_jpushRegistrationIdCompleter.isCompleted && !isNullOrBlank) {
if (!(_jpushRegistrationIdCompleter?.isCompleted ?? true) &&
!isNullOrBlank) {
await Storage.setString(pushDeviceID, data['message']);
AppLog.log('flutter get registration id : ${data['message']}');
_jpushRegistrationIdCompleter.complete(<String, dynamic>{
_jpushRegistrationIdCompleter?.complete(<String, dynamic>{
'channel': 'jiguang',
'channelToken': data['message']
});
final String? channel2TokenStr =
await Storage.getString(vendorPushChannelInfo);
if (Platform.isAndroid &&
!(_vendorTokenCompleter?.isCompleted ?? true) &&
channel2TokenStr != null) {
_vendorTokenCompleter?.complete(jsonDecode(channel2TokenStr));
}
}
// final String? channel2TokenStr =
// await Storage.getString(vendorPushChannelInfo);
// if (Platform.isAndroid && channel2TokenStr != null) {
// _vendorTokenCompleter.complete(jsonDecode(channel2TokenStr));
// }
break;
case CMD_GET_TOKEN:
final bool isNullOrBlank =
GetUtils.isNullOrBlank(data['token']) ?? true;
if (!_vendorTokenCompleter.isCompleted && !isNullOrBlank) {
if (!(_vendorTokenCompleter?.isCompleted ?? true) &&
!isNullOrBlank) {
final Map<String, dynamic> channel2Token = <String, dynamic>{
'channel': channelTypeMapping[data['platform']],
'channelToken': data['token']
};
await Storage.setString(
vendorPushChannelInfo, jsonEncode(channel2Token));
_vendorTokenCompleter.complete(channel2Token);
_vendorTokenCompleter?.complete(channel2Token);
}
break;
}
},
onReceiveNotification: (Map<String, dynamic> message) async {
AppLog.log('onReceiveNotification: $message');
debugPrint("addJPushEventHandler onReceiveNotification:$message");
debugPrint('addJPushEventHandler onReceiveNotification:$message');
// showCustomNotification(message);
},
onOpenNotification: (Map<String, dynamic> message) async {
@ -150,9 +153,15 @@ class XSJPushProvider {
// jpush
Future<void> bindPushChannels() async {
try {
if (_jpushRegistrationIdCompleter == null ||
_vendorTokenCompleter == null) {
return;
}
debugPrint("await PushChannels start");
var channels = await Future.wait(
[_jpushRegistrationIdCompleter.future, _vendorTokenCompleter.future]);
final List<Map<String, dynamic>> channels = await Future.wait([
_jpushRegistrationIdCompleter!.future,
_vendorTokenCompleter!.future
]);
final String? registrationId = await Storage.getString(pushDeviceID);
debugPrint("await PushChannels end");
final MineUnbindPhoneOrEmailEntity entity =

View File

@ -152,10 +152,11 @@ class Storage {
//
static Future<void> clearAll() async {
final SharedPreferences sp = await SharedPreferences.getInstance();
sp.clear();
// sp.clear();
sp.remove(saveUserLoginData);
//
//退
await setString(isAgreePrivacy, isAgreePrivacy);
// await setString(isAgreePrivacy, isAgreePrivacy);
}
static Future<String?> getUid() async {