Implemented firebase and onesignal

This commit is contained in:
Abdullah Alassaf
2024-05-23 17:09:10 +03:00
parent f7db91f212
commit cda41ecf74
17 changed files with 312 additions and 64 deletions
@@ -1,8 +1,14 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/app_layout/view/widgets/app_bar_home_dropdown.dart';
import 'package:syncrow_app/features/auth/model/user_model.dart';
import 'package:syncrow_app/features/dashboard/view/dashboard_view.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
@@ -23,6 +29,7 @@ part 'home_state.dart';
class HomeCubit extends Cubit<HomeState> {
HomeCubit._() : super(HomeInitial()) {
checkIfNotificationPermissionGranted();
if (selectedSpace == null) {
fetchUnitsByUserId().then((value) {
if (selectedSpace != null) {
@@ -52,6 +59,9 @@ class HomeCubit extends Cubit<HomeState> {
selectedSpace = null;
selectedRoom = null;
pageIndex = 0;
OneSignal.User.pushSubscription.removeObserver((stateChanges) => oneSignalSubscriptionObserver);
OneSignal.Notifications.removePermissionObserver((permission) => oneSignalPermissionObserver);
OneSignal.Notifications.removeClickListener((event) => oneSignalClickListenerObserver);
return super.close();
}
@@ -69,11 +79,67 @@ class HomeCubit extends Cubit<HomeState> {
var duration = const Duration(milliseconds: 300);
void oneSignalPermissionObserver;
void oneSignalSubscriptionObserver;
void oneSignalClickListenerObserver;
// selectSpace(SpaceModel space) async {
// selectedSpace = space;
// emit(SpaceSelected(space));
// }
checkIfNotificationPermissionGranted() async {
try {
OneSignal.initialize('762350c9-1e5d-4d95-a648-16d4dc8a25e1');
//Show native push notification dialog
if (Platform.isIOS) {
await OneSignal.Notifications.permissionNative();
} else {
await OneSignal.Notifications.requestPermission(true);
}
if (await Permission.notification.isGranted == false) {
return;
}
var userUuid = await const FlutterSecureStorage().read(key: UserModel.userUuidKey) ?? '';
if (userUuid.isNotEmpty) {
await OneSignal.login(userUuid);
}
//Enable push notifications
await OneSignal.User.pushSubscription.optIn();
//this function will be called once a user is subscribed
oneSignalSubscriptionObserver = OneSignal.User.pushSubscription.addObserver((state) async {
if (state.current.optedIn) {
await _sendSubscriptionId();
}
});
// Send the player id when a user allows notifications
oneSignalPermissionObserver = OneSignal.Notifications.addPermissionObserver((state) async {
await _sendSubscriptionId();
});
//check if the player id is sent, if not send it again
await _sendSubscriptionId();
oneSignalClickListenerObserver = OneSignal.Notifications.addClickListener((event) async {
//Once the user clicks on the notification
});
} catch (err) {
debugPrint("******* Error");
debugPrint(err.toString());
rethrow;
}
}
_sendSubscriptionId() async {
String? subscriptionId = OneSignal.User.pushSubscription.id ?? '';
//TODO send the subscription id to BE
}
changeSelectedSpace(SpaceModel space) {
selectedSpace = space;
emitSafe(SpaceSelected(space));
@@ -1,5 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart';
@@ -157,19 +159,22 @@ class CeilingSensorInterface extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: [
DefaultContainer(
padding:
const EdgeInsets.symmetric(vertical: 20, horizontal: 15),
padding: const EdgeInsets.symmetric(
vertical: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
const BodySmall(text: 'Sports Para'),
BodyLarge(
text: '0',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
const Flexible(child: BodySmall(text: 'Sports Para')),
Flexible(
child: BodyLarge(
text: '0',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
),
),
),
],
@@ -185,11 +190,18 @@ class CeilingSensorInterface extends StatelessWidget {
Column(
mainAxisSize: MainAxisSize.min,
children: [
const BodySmall(text: 'Detection Range'),
BodyLarge(
text: '0.0M',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
const Flexible(
child: BodySmall(
text: 'Detection Range',
textOverflow: TextOverflow.ellipsis,
)),
Flexible(
child: BodyLarge(
text: '0.0M',
textOverflow: TextOverflow.ellipsis,
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
),
),
),
],
@@ -205,11 +217,18 @@ class CeilingSensorInterface extends StatelessWidget {
Column(
mainAxisSize: MainAxisSize.min,
children: [
const BodySmall(text: 'Movement'),
BodyLarge(
text: 'none',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
const Flexible(
child: BodySmall(
text: 'Movement',
textOverflow: TextOverflow.ellipsis,
)),
Flexible(
child: BodyLarge(
text: 'none',
textOverflow: TextOverflow.ellipsis,
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
),
),
),
],
@@ -4,16 +4,16 @@ import 'package:syncrow_app/utils/context_extension.dart';
import 'custom_text_widget.dart';
class BodyLarge extends StatelessWidget {
const BodyLarge({
required this.text,
super.key,
this.textAlign,
this.style,
this.height,
this.fontWeight,
this.fontColor,
this.fontSize,
});
const BodyLarge(
{required this.text,
super.key,
this.textAlign,
this.style,
this.height,
this.fontWeight,
this.fontColor,
this.fontSize,
this.textOverflow});
final String text;
final TextAlign? textAlign;
@@ -28,16 +28,18 @@ class BodyLarge extends StatelessWidget {
final double? fontSize;
final TextOverflow? textOverflow;
@override
Widget build(BuildContext context) => CustomText(
text,
textAlign: textAlign,
style: style ??
context.bodyLarge.copyWith(
height: height,
fontWeight: fontWeight,
color: fontColor,
fontSize: fontSize,
),
height: height,
fontWeight: fontWeight,
color: fontColor,
fontSize: fontSize,
overflow: textOverflow),
);
}
@@ -11,6 +11,7 @@ class BodySmall extends StatelessWidget {
this.fontSize,
this.fontWeight,
this.textAlign,
this.textOverflow,
});
final String text;
@@ -22,6 +23,7 @@ class BodySmall extends StatelessWidget {
final FontWeight? fontWeight;
final TextAlign? textAlign;
final TextOverflow? textOverflow;
@override
Widget build(BuildContext context) => CustomText(
@@ -31,5 +33,6 @@ class BodySmall extends StatelessWidget {
fontSize: fontSize,
fontWeight: fontWeight,
textAlign: textAlign,
textOverflow: textOverflow,
);
}
@@ -9,6 +9,7 @@ class CustomText extends StatelessWidget {
this.minLines,
this.maxLines,
this.textDirection,
this.textOverflow,
this.fontSize,
this.fontColor,
this.fontWeight});
@@ -20,6 +21,7 @@ class CustomText extends StatelessWidget {
final int? minLines;
final int? maxLines;
final TextDirection? textDirection;
final TextOverflow? textOverflow;
final double? fontSize;
final Color? fontColor;
@@ -36,6 +38,7 @@ class CustomText extends StatelessWidget {
textAlign: textAlign,
// onTap: onTap,
// minLines: minLines,
overflow: textOverflow,
maxLines: maxLines,
textDirection: textDirection,
);
+25 -28
View File
@@ -1,5 +1,5 @@
// File generated by FlutterFire CLI.
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
// ignore_for_file: type=lint
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;
@@ -17,10 +17,7 @@ import 'package:flutter/foundation.dart'
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for web - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
return web;
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
@@ -50,30 +47,30 @@ class DefaultFirebaseOptions {
}
static const FirebaseOptions android = FirebaseOptions(
apiKey: '',
//'AIzaSyDXEMzUFunmu9RX9bbzwKldLDgxhPXyCyg',
appId: '',
//'1:922241269489:android:2b51e077ee59a8da5d7350',
messagingSenderId: '',
//'922241269489',
projectId: '',
// 'realkeyper-v3',
storageBucket: '', // 'realkeyper-v3.appspot.com',
apiKey: 'AIzaSyA5qOErxdm0zJmoHIB0TixfebYEsNRpwV0',
appId: '1:427332280600:android:bb6047adeeb80fb00c7e6d',
messagingSenderId: '427332280600',
projectId: 'test2-8a3d2',
storageBucket: 'test2-8a3d2.appspot.com',
);
static const FirebaseOptions ios = FirebaseOptions(
apiKey: '',
//'AIzaSyD2r7wkhuYKTRH-FM-6PVNfzqE865ASYys',
appId: '',
//'1:922241269489:ios:9a43e191466515c65d7350',
messagingSenderId: '',
//'922241269489',
projectId: '',
//'realkeyper-v3',
storageBucket: '',
//'realkeyper-v3.appspot.com',
iosClientId: '',
//'922241269489-01hod7jlea19gg983v0b1nfmp7ce6p0v.apps.googleusercontent.com',
iosBundleId: '', //'com.realkeyper.investorapp2',
apiKey: 'AIzaSyABnpH6yo2RRjtkp4PlvtK84hKwRm2DhBw',
appId: '1:427332280600:ios:373a65cce55a3af40c7e6d',
messagingSenderId: '427332280600',
projectId: 'test2-8a3d2',
storageBucket: 'test2-8a3d2.appspot.com',
iosBundleId: 'com.example.syncrowApp',
);
}
static const FirebaseOptions web = FirebaseOptions(
apiKey: 'AIzaSyCVEvKsJYzhWDFM-9Od68FE0nPpP933st0',
appId: '1:427332280600:web:ad50516a87a35a1a0c7e6d',
messagingSenderId: '427332280600',
projectId: 'test2-8a3d2',
authDomain: 'test2-8a3d2.firebaseapp.com',
storageBucket: 'test2-8a3d2.appspot.com',
measurementId: 'G-Z1RTTTV5H9',
);
}
+7 -1
View File
@@ -1,7 +1,8 @@
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/firebase_options.dart';
import 'package:syncrow_app/services/locator.dart';
import 'package:syncrow_app/utils/bloc_observer.dart';
import 'package:syncrow_app/utils/helpers/localization_helpers.dart';
@@ -32,6 +33,11 @@ void main() {
//to initialize the locator
initialSetup();
await Firebase.initializeApp(
name: 'test2',
options: DefaultFirebaseOptions.currentPlatform,
);
//final SharedPreferences prefs = await SharedPreferences.getInstance();
//to save the locale in the shared preferences