mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
Implemented firebase and onesignal
This commit is contained in:
@ -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));
|
||||
|
Reference in New Issue
Block a user