More work with fire notifications
This commit is contained in:
parent
5fdaf239c0
commit
9dd8a7de97
24 changed files with 512 additions and 288 deletions
|
|
@ -1,23 +1,38 @@
|
|||
import 'package:bson_objectid/bson_objectid.dart';
|
||||
import 'package:comunes_flutter/comunes_flutter.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:fires_flutter/models/fireNotification.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_redux/flutter_redux.dart';
|
||||
import 'package:redux/redux.dart';
|
||||
import 'package:redux/src/store.dart';
|
||||
|
||||
import 'activeFires.dart';
|
||||
import 'fireAlert.dart';
|
||||
import 'fireNotificationList.dart';
|
||||
import 'generated/i18n.dart';
|
||||
import 'homePage.dart';
|
||||
import 'introPage.dart';
|
||||
import 'models/appState.dart';
|
||||
import 'privacyPage.dart';
|
||||
import 'redux/actions.dart';
|
||||
import 'sandbox.dart';
|
||||
import 'theme.dart';
|
||||
import 'privacyPage.dart';
|
||||
import 'fireAlert.dart';
|
||||
import 'supportPage.dart';
|
||||
import 'fireNotificationList.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
class FiresApp extends StatelessWidget {
|
||||
class FiresApp extends StatefulWidget {
|
||||
FiresApp(this.store);
|
||||
|
||||
final Store<AppState> store;
|
||||
|
||||
@override
|
||||
_FiresAppState createState() => _FiresAppState(store);
|
||||
}
|
||||
|
||||
class _FiresAppState extends State<FiresApp> {
|
||||
final GlobalKey<NavigatorState> navigatorKey =
|
||||
new GlobalKey<NavigatorState>();
|
||||
static final WidgetBuilder introWidget = (context) => new IntroPage();
|
||||
static final WidgetBuilder continueWidget = (context) => new HomePage();
|
||||
|
||||
|
|
@ -29,22 +44,23 @@ class FiresApp extends StatelessWidget {
|
|||
Sandbox.routeName: (BuildContext context) => new Sandbox(),
|
||||
FireAlert.routeName: (BuildContext context) => new FireAlert(),
|
||||
SupportPage.routeName: (BuildContext context) => new SupportPage(),
|
||||
FireNotificationList.routeName: (BuildContext context) => new FireNotificationList(),
|
||||
FireNotificationList.routeName: (BuildContext context) =>
|
||||
new FireNotificationList(),
|
||||
};
|
||||
|
||||
final Store<AppState> store;
|
||||
|
||||
// globals.getIt.registerSingleton
|
||||
FiresApp(this.store);
|
||||
_FiresAppState(this.store);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
StatefulWidget home = new MaterialAppWithIntroHome(
|
||||
introWidget, continueWidget, 'showInitialWizard-2018-06-27-01');
|
||||
return new StoreProvider<AppState>(
|
||||
|
||||
store: this.store,
|
||||
child: new MaterialApp(
|
||||
navigatorKey: navigatorKey,
|
||||
localizationsDelegates: [
|
||||
S.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
|
|
@ -65,4 +81,97 @@ class FiresApp extends StatelessWidget {
|
|||
theme: firesTheme,
|
||||
routes: routes));
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
|
||||
print("onMessage in fireApp: $message");
|
||||
_showItemDialog(message, store);
|
||||
}, onLaunch: (Map<String, dynamic> message) {
|
||||
print("onLaunch: $message");
|
||||
_navigateToItemDetail(message, store);
|
||||
}, onResume: (Map<String, dynamic> message) {
|
||||
print("onResume: $message");
|
||||
_navigateToItemDetail(message, store);
|
||||
});
|
||||
_firebaseMessaging.requestNotificationPermissions(
|
||||
const IosNotificationSettings(sound: true, badge: true, alert: true));
|
||||
_firebaseMessaging.onIosSettingsRegistered
|
||||
.listen((IosNotificationSettings settings) {
|
||||
print("Settings registered: $settings");
|
||||
});
|
||||
_firebaseMessaging.getToken().then((String token) {
|
||||
assert(token != null);
|
||||
store.dispatch(new OnUserTokenAction(token));
|
||||
setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
void _showItemDialog(Map<String, dynamic> message, Store store) {
|
||||
final notif = _notifForMessage(message);
|
||||
showDialog<bool>(
|
||||
context: navigatorKey.currentContext,
|
||||
builder: (_) => _buildDialog(navigatorKey.currentContext, notif),
|
||||
).then((bool shouldNavigate) {
|
||||
if (shouldNavigate == true) {
|
||||
_navigateToItemDetail(message, store);
|
||||
}
|
||||
}).catchError((e) => print("$e"));
|
||||
}
|
||||
|
||||
Widget _buildDialog(BuildContext context, FireNotification item) {
|
||||
return new AlertDialog(
|
||||
content: new Text(item.description),
|
||||
actions: <Widget>[
|
||||
new FlatButton(
|
||||
child: Text(S.of(context).CLOSE),
|
||||
onPressed: () {
|
||||
Navigator.pop(context, false);
|
||||
},
|
||||
),
|
||||
new FlatButton(
|
||||
child: Text(S.of(context).SHOW),
|
||||
onPressed: () {
|
||||
Navigator.pop(context, true);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _navigateToItemDetail(Map<String, dynamic> message, Store store) {
|
||||
FireNotification notif = _notifForMessage(message);
|
||||
// Clear away dialogs
|
||||
Navigator.popUntil(navigatorKey.currentContext,
|
||||
(Route<dynamic> route) => route is PageRoute);
|
||||
if (!notif.getRoute(store).isCurrent) {
|
||||
Navigator.push(navigatorKey.currentContext, notif.getRoute(store));
|
||||
}
|
||||
}
|
||||
|
||||
// https://pub.dartlang.org/packages/firebase_messaging#-example-tab-
|
||||
final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
|
||||
|
||||
FireNotification _notifForMessage(Map<String, dynamic> message) {
|
||||
FireNotification notif;
|
||||
print('start parse notif');
|
||||
try {
|
||||
notif = new FireNotification(
|
||||
id: new ObjectId.fromHexString(message['id']),
|
||||
lat: double.parse(message['lat']),
|
||||
lon: double.parse(message['lon']),
|
||||
description: message['description'],
|
||||
read: false,
|
||||
when: DateTime.parse(message['when']),
|
||||
sealed: message['sealed'],
|
||||
subsId: new ObjectId.fromHexString(message['subsId']));
|
||||
print('end parse notif');
|
||||
debugPrint(notif.toString());
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
}
|
||||
store.dispatch(new AddFireNotificationAction(notif));
|
||||
return notif;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue