forked from eatvio/eatvio_app
Merge remote-tracking branch 'upstream/main' into issue-710
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
import 'package:eatvio_app/core/utils/toasts.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/core/token_storage.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:huawei_push/huawei_push.dart';
|
||||
import 'package:flutter/services.dart' show MethodChannel, PlatformException;
|
||||
|
||||
class FirebaseApi {
|
||||
final _firebaseMessaging = FirebaseMessaging.instance;
|
||||
|
||||
Future<String?> getToken() async {
|
||||
await _firebaseMessaging.requestPermission();
|
||||
String? token;
|
||||
|
||||
String? token = await _firebaseMessaging.getToken();
|
||||
|
||||
if (token == null && Platform.isAndroid) {
|
||||
final isHuawei = await _isHuaweiDevice();
|
||||
if (isHuawei) {
|
||||
token = await _getHuaweiPushToken();
|
||||
}
|
||||
if (await _isHuaweiDevice()) {
|
||||
token = await _getHuaweiPushTokenNative();
|
||||
} else {
|
||||
await _firebaseMessaging.requestPermission();
|
||||
token = await _firebaseMessaging.getToken();
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
@@ -30,10 +29,11 @@ class FirebaseApi {
|
||||
if (await TokenStorage.haveTokenInStorage()) {
|
||||
try {
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
final isHuawei = Platform.isAndroid ? await _isHuaweiDevice() : false;
|
||||
await sl<Dio>().post(
|
||||
'/api/profile/notification/token/register',
|
||||
data: {
|
||||
'platform': Platform.isIOS ? 'ios' : 'android',
|
||||
'platform': Platform.isIOS ? 'ios' : (isHuawei ? 'huawei' : 'android'),
|
||||
'hw_id': Platform.isIOS ? (await deviceInfo.iosInfo).identifierForVendor : (await deviceInfo.androidInfo).id,
|
||||
'token': await getToken(),
|
||||
},
|
||||
@@ -47,33 +47,27 @@ class FirebaseApi {
|
||||
Future<bool> _isHuaweiDevice() async {
|
||||
if (!Platform.isAndroid) return false;
|
||||
try {
|
||||
final android = await DeviceInfoPlugin().androidInfo;
|
||||
final brand = android.brand.toLowerCase();
|
||||
final manufacturer = android.manufacturer.toLowerCase();
|
||||
return brand.contains('huawei') || manufacturer.contains('huawei') || brand.contains('honor') || manufacturer.contains('honor');
|
||||
final info = await DeviceInfoPlugin().androidInfo;
|
||||
final brand = info.brand.toLowerCase();
|
||||
final manufacturer = info.manufacturer.toLowerCase();
|
||||
return brand.contains('huawei') || brand.contains('honor') || manufacturer.contains('huawei') || manufacturer.contains('honor');
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> _getHuaweiPushToken() async {
|
||||
Future<String?> _getHuaweiPushTokenNative() async {
|
||||
try {
|
||||
await Push.turnOnPush();
|
||||
|
||||
String? resolvedToken;
|
||||
final sub = Push.getTokenStream.listen((t) => resolvedToken ??= t, onError: (_) {});
|
||||
|
||||
Push.getToken('');
|
||||
|
||||
final started = DateTime.now();
|
||||
while (resolvedToken == null && DateTime.now().difference(started).inSeconds < 5) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
await sub.cancel();
|
||||
return resolvedToken;
|
||||
const channel = MethodChannel('com.eatvioapp/hms_push');
|
||||
final String? token = await channel.invokeMethod<String>('getHuaweiToken');
|
||||
return token;
|
||||
} on PlatformException catch (e, st) {
|
||||
showErrorToast('$e $st');
|
||||
log(e.toString(), stackTrace: st);
|
||||
return null;
|
||||
} catch (e, st) {
|
||||
log('Huawei Push token error: $e', stackTrace: st);
|
||||
showErrorToast('$e $st');
|
||||
log(e.toString(), stackTrace: st);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user