Created
August 8, 2019 15:48
-
-
Save bmelton/4eed490df0dece5815a7075e96a18347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_messaging/firebase_messaging.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:myapp/database/database.dart'; | |
import 'package:myapp/screens/home.dart'; | |
import 'package:myapp/screens/list_words.dart'; | |
import 'package:myapp/services/locator.dart'; | |
import 'package:myapp/screens/settings.dart'; | |
import 'package:myapp/database/models.dart'; | |
void main() { | |
setupLocator(); | |
runApp(MyApp()); | |
} | |
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); | |
final WordDatabase db = WordDatabase(); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
void firebaseCloudMessagingListeners() { | |
_firebaseMessaging.getToken().then((token){ | |
}); | |
_firebaseMessaging.configure( | |
onMessage: (Map<String, dynamic> message) async { | |
Future<bool> didUpdate = setupAnyway(db); | |
print(message.toString()); | |
showDialog( | |
context: context, | |
builder: (context) => AlertDialog( | |
content: ListTile( | |
title: Text(message['notification']['title']), | |
subtitle: Text(message['notification']['body']), | |
), | |
actions: <Widget>[ | |
FlatButton( | |
child: Text('Ok'), | |
onPressed: () => Navigator.of(context).pop(), | |
), | |
], | |
), | |
); | |
print(didUpdate.toString()); | |
}, | |
onResume: (Map<String, dynamic> message) async { | |
Future<bool> didUpdate = setupAnyway(db); | |
print(didUpdate.toString()); | |
}, | |
onLaunch: (Map<String, dynamic> message) async { | |
Future<bool> didUpdate = setupAnyway(db); | |
print(didUpdate.toString()); | |
}, | |
); | |
} | |
void subscribeWordNotifications() async { | |
Preference notificationPref = await db.fetchNotificationPreference('xyztopic'); | |
if(notificationPref.value == 1) { | |
_firebaseMessaging.subscribeToTopic("xyztopic"); | |
} else { | |
_firebaseMessaging.unsubscribeFromTopic("xyztopic"); | |
} | |
} | |
@override | |
void initState() { | |
super.initState(); | |
_firebaseMessaging.requestNotificationPermissions(); | |
subscribeWordNotifications(); | |
firebaseCloudMessagingListeners(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'MyApp', | |
theme: ThemeData( | |
primarySwatch: Colors.pink, | |
primaryColor: Colors.pink, | |
accentColor: Colors.pink[600], | |
hintColor: Colors.pink[100], | |
textTheme: TextTheme( | |
display2: TextStyle( | |
fontSize: 44.0, | |
fontFamily: "Roboto Slab", | |
fontWeight: FontWeight.bold, | |
color: Colors.white, | |
), | |
subtitle: TextStyle(fontSize: 12.0, color: Colors.grey[100]), | |
caption: TextStyle(fontSize: 14.0), | |
body1: TextStyle(fontSize: 16.0), | |
), | |
), | |
debugShowCheckedModeBanner: false, | |
initialRoute: '/', | |
routes: { | |
'/': (context) => MyHomePage(title: 'My App'), | |
'/settings': (context) => SettingsPage(), | |
'/list': (context) => List(), | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment