Skip to content

Commit 273abc8

Browse files
committed
changes
0 parents  commit 273abc8

File tree

14 files changed

+790
-0
lines changed

14 files changed

+790
-0
lines changed

.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
33+
# Android related
34+
**/android/**/gradle-wrapper.jar
35+
**/android/.gradle
36+
**/android/captures/
37+
**/android/gradlew
38+
**/android/gradlew.bat
39+
**/android/local.properties
40+
**/android/**/GeneratedPluginRegistrant.java
41+
42+
# iOS/XCode related
43+
**/ios/**/*.mode1v3
44+
**/ios/**/*.mode2v3
45+
**/ios/**/*.moved-aside
46+
**/ios/**/*.pbxuser
47+
**/ios/**/*.perspectivev3
48+
**/ios/**/*sync/
49+
**/ios/**/.sconsign.dblite
50+
**/ios/**/.tags*
51+
**/ios/**/.vagrant/
52+
**/ios/**/DerivedData/
53+
**/ios/**/Icon?
54+
**/ios/**/Pods/
55+
**/ios/**/.symlinks/
56+
**/ios/**/profile
57+
**/ios/**/xcuserdata
58+
**/ios/.generated/
59+
**/ios/Flutter/App.framework
60+
**/ios/Flutter/Flutter.framework
61+
**/ios/Flutter/Flutter.podspec
62+
**/ios/Flutter/Generated.xcconfig
63+
**/ios/Flutter/app.flx
64+
**/ios/Flutter/app.zip
65+
**/ios/Flutter/flutter_assets/
66+
**/ios/Flutter/flutter_export_environment.sh
67+
**/ios/ServiceDefinitions.json
68+
**/ios/Runner/GeneratedPluginRegistrant.*
69+
70+
# Exceptions to above rules.
71+
!**/ios/**/default.mode1v3
72+
!**/ios/**/default.mode2v3
73+
!**/ios/**/default.pbxuser
74+
!**/ios/**/default.perspectivev3

.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 78910062997c3a836feee883712c241a5fd22983
8+
channel: stable
9+
10+
project_type: package

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [0.0.1] - 01-04-2022
2+
3+
* Initial release.

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# laravel_notify_fcm
2+
3+
A new Flutter package.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Dart
8+
[package](https://flutter.dev/developing-packages/),
9+
a library module containing code that can be shared easily across
10+
multiple Flutter or Dart projects.
11+
12+
For help getting started with Flutter, view our
13+
[online documentation](https://flutter.dev/docs), which offers tutorials,
14+
samples, guidance on mobile development, and a full API reference.

lib/helpers/tools.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:shared_preferences/shared_preferences.dart';
2+
3+
Future<String?> getStorage({String key = 'key'}) async {
4+
SharedPreferences prefs = await SharedPreferences.getInstance();
5+
return prefs.getString(key);
6+
}
7+
8+
Future<bool> storeStorage(String dynamic, {String key = 'key'}) async {
9+
SharedPreferences prefs = await SharedPreferences.getInstance();
10+
return await prefs.setString(key, dynamic);
11+
}

lib/interceptor_fcm_request.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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('[^\u0001-\u007F]'), '_'),
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('[^\u0001-\u007F]'), '_'),
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+
}

lib/laravel_notify_fcm.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library laravel_notify_fcm;
2+
3+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import 'dart:io';
2+
3+
import 'package:firebase_messaging/firebase_messaging.dart';
4+
import 'package:flutter/material.dart';
5+
import 'package:laravel_notify_fcm/services/apis/laravel_notify_fcm/laravel_notify_fcm_api_service.dart';
6+
7+
class FCMListener {
8+
9+
FCMListener(this.firebaseMessaging);
10+
11+
FirebaseMessaging? firebaseMessaging;
12+
13+
onMessage(Function(RemoteMessage message) onMessage) {
14+
FirebaseMessaging.onMessage.listen((message) async {
15+
await onMessage(message);
16+
});
17+
}
18+
19+
onBackgroundMessage(Function(RemoteMessage message) onMessage) {
20+
FirebaseMessaging.onBackgroundMessage((message) => onMessage(message));
21+
}
22+
}
23+
24+
class LaravelNotifyFCMService {
25+
LaravelNotifyFCMService._privateConstructor();
26+
27+
static final LaravelNotifyFCMService instance = LaravelNotifyFCMService._privateConstructor();
28+
29+
FCMListener? listen;
30+
late String siteUrl;
31+
String? token;
32+
late ApiService _apiService;
33+
34+
FirebaseMessaging? _firebaseMessaging;
35+
36+
init({required siteUrl, required FirebaseMessaging? firebaseMessaging}) {
37+
this.siteUrl = siteUrl;
38+
_apiService = ApiService(this.siteUrl);
39+
_firebaseMessaging = firebaseMessaging;
40+
listen = FCMListener(firebaseMessaging);
41+
}
42+
43+
Future<String?> requestToSendNotifications() async {
44+
if (Platform.isIOS) {
45+
await _firebaseMessaging!.requestPermission();
46+
}
47+
String? token = await _firebaseMessaging!.getToken();
48+
this.token = token;
49+
return token;
50+
}
51+
52+
Future<dynamic> sendToken({required String sanctumToken}) async {
53+
return await _apiService.sendPushToken(sanctumToken: sanctumToken);
54+
}
55+
56+
Future<dynamic> updateToken(active, {required String sanctumToken}) async {
57+
return await _apiService.updatePushToken(active: active, sanctumToken: sanctumToken);
58+
}
59+
60+
FirebaseMessaging? getFirebase() {
61+
return _firebaseMessaging;
62+
}
63+
}

lib/services/apis/base_api.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// StoreMob
2+
//
3+
// Created by Anthony Gordon.
4+
// 2020, WooSignal Ltd. All rights reserved.
5+
//
6+
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
11+
import 'package:dio/dio.dart';
12+
import 'package:laravel_notify_fcm/interceptor_fcm_request.dart';
13+
14+
/// Base class for handling API networking
15+
abstract class BaseApi {
16+
late Dio client;
17+
bool debugMode;
18+
19+
BaseApi({this.debugMode = false}) {
20+
client = new Dio(this._getOptions());
21+
22+
client.interceptors
23+
.add(InterceptorNotifyFCM());
24+
}
25+
26+
BaseOptions _getOptions() {
27+
return BaseOptions(
28+
connectTimeout: 10000,
29+
receiveTimeout: 7000,
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)