forked from eatvio/eatvio_app
95 lines
3.1 KiB
Dart
95 lines
3.1 KiB
Dart
import 'dart:developer';
|
|
import 'dart:io';
|
|
import 'package:eatvio_app/core/app_config.dart';
|
|
import 'package:eatvio_app/core/app_initializer.dart';
|
|
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:flutter/services.dart' show MethodChannel, PlatformException;
|
|
|
|
class FirebaseApi {
|
|
final _firebaseMessaging = FirebaseMessaging.instance;
|
|
|
|
Future<String?> getToken() async {
|
|
String? token;
|
|
|
|
if (await _isHuaweiDevice()) {
|
|
token = await _getHuaweiPushTokenNative();
|
|
} else {
|
|
if ((sl<AppConfig>().buildType == AppBuildType.prod)) {
|
|
await _firebaseMessaging.requestPermission();
|
|
}
|
|
token = await _firebaseMessaging.getToken();
|
|
}
|
|
return token;
|
|
}
|
|
|
|
Future<void> registerAnonymousPushToken() async {
|
|
try {
|
|
final deviceInfo = DeviceInfoPlugin();
|
|
await sl<Dio>().post(
|
|
'/api/anonymous_token',
|
|
data: {
|
|
'platform': Platform.isIOS ? 'ios' : 'android',
|
|
'hw_id': Platform.isIOS ? (await deviceInfo.iosInfo).identifierForVendor : (await deviceInfo.androidInfo).id,
|
|
'token': await getToken(),
|
|
},
|
|
);
|
|
} catch (e, st) {
|
|
log(e.toString(), stackTrace: st);
|
|
}
|
|
}
|
|
|
|
Future<void> registerPushToken() async {
|
|
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' : (isHuawei ? 'huawei' : 'android'),
|
|
'hw_id': Platform.isIOS ? (await deviceInfo.iosInfo).identifierForVendor : (await deviceInfo.androidInfo).id,
|
|
'token': await getToken(),
|
|
},
|
|
);
|
|
} catch (e, st) {
|
|
log(e.toString(), stackTrace: st);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<bool> _isHuaweiDevice() async {
|
|
if (!Platform.isAndroid) return false;
|
|
try {
|
|
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?> _getHuaweiPushTokenNative() async {
|
|
try {
|
|
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) {
|
|
showErrorToast('$e $st');
|
|
log(e.toString(), stackTrace: st);
|
|
return null;
|
|
}
|
|
}
|
|
}
|