1+ import 'dart:convert' ;
2+ import 'dart:io' ;
3+
4+ import 'package:device_info_plus/device_info_plus.dart' ;
5+ import 'package:dio/dio.dart' ;
6+ import 'package:laravel_notify_fcm/helpers/tools.dart' ;
7+ import 'package:uuid/uuid.dart' ;
8+
9+ class InterceptorNotifyFCM extends InterceptorsWrapper {
10+
11+ InterceptorNotifyFCM ();
12+
13+ @override
14+ Future onRequest (RequestOptions options, RequestInterceptorHandler handler) async {
15+ options.headers.addAll (await this ._xMetaData ());
16+ return handler.next (options);
17+ }
18+
19+ Future <Map <String , dynamic >> _xMetaData () async {
20+ Map <String , dynamic > headers = {};
21+ Map <String , String ?> userDeviceMeta = await this ._getUserMeta ();
22+ headers.addAll ({"X-DMETA" : jsonEncode (userDeviceMeta)});
23+ return headers;
24+ }
25+
26+ Future <Map <String , String ?>> _getUserMeta () async {
27+ String uuid = await _getUUID ();
28+
29+ DeviceInfoPlugin deviceInfo = DeviceInfoPlugin ();
30+
31+ Map <String , String ?> data = {
32+ "uuid" : uuid,
33+ };
34+ if (Platform .isAndroid) {
35+ AndroidDeviceInfo androidMeta = await deviceInfo.androidInfo;
36+ data.addAll ({
37+ "model" : androidMeta.product.toString (),
38+ "display_name" :
39+ androidMeta.brand! .replaceAll (new RegExp ('[^\u 0001-\u 007F]' ), '_' ),
40+ "platform" : "android" ,
41+ "version" : androidMeta.version.release,
42+ });
43+ } else if (Platform .isIOS) {
44+ IosDeviceInfo iosMeta = await deviceInfo.iosInfo;
45+ data.addAll ({
46+ "model" : iosMeta.model,
47+ "display_name" : iosMeta.name! .replaceAll (new RegExp ('[^\u 0001-\u 007F]' ), '_' ),
48+ "platform" : "ios" ,
49+ "version" : iosMeta.systemVersion.toString (),
50+ });
51+ }
52+ return data;
53+ }
54+
55+ Future <String > _getUUID () async {
56+ String ? uuid = await getStorage (key: 'fcm_app_uuid' );
57+ if (uuid == null ) {
58+ var newUUID = new Uuid ();
59+ uuid = newUUID.v1 ();
60+ await storeStorage (uuid, key: 'fcm_app_uuid' );
61+ }
62+ return uuid;
63+ }
64+ }
0 commit comments