Skip to content

Instantly share code, notes, and snippets.

@bmelton
Created August 8, 2019 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmelton/4eed490df0dece5815a7075e96a18347 to your computer and use it in GitHub Desktop.
Save bmelton/4eed490df0dece5815a7075e96a18347 to your computer and use it in GitHub Desktop.
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