-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
111 lines (100 loc) · 3.08 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:wallie/screens/home_page.dart';
import 'package:wallie/screens/login_screen.dart';
import 'config/configuration.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Backdrop',
theme: ThemeData(
fontFamily: "productsans",
brightness: Brightness.dark,
primaryColor: primaryColor,
),
debugShowCheckedModeBanner: false,
home: MainApp(),
);
}
}
class MainApp extends StatefulWidget {
@override
_MainAppState createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
void initState() {
_firebaseMessaging.configure(
onMessage: (Map <String, dynamic> message) async {
print("onMessage: $message");
String title = message["notification"]["title"] ?? "";
String body = message["notification"]["body"] ?? "";
print(title);
print(body);
displayDialog(
title: title,
body: body,
);
},
onLaunch: (Map <String, dynamic> message) async {
print("onMessage: $message");
String title = message["notification"]["title"] ?? "";
String body = message["notification"]["body"] ?? "";
print(title);
print(body);
displayDialog(
title: title,
body: body,
);
},
onResume: (Map <String, dynamic> message) async {
print("onMessage: $message");
String title = message["notification"]["title"] ?? "";
String body = message["notification"]["body"] ?? "";
print(title);
print(body);
displayDialog(
title: title,
body: body,
);
},
);
_firebaseMessaging.subscribeToTopic("promotion");
super.initState();
}
Future displayDialog({String title, String body}) {
return showDialog(context: context, builder: (ctx) {
return AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(50),),
title: Text(title),
content: Text(body),
actions: <Widget>[
FlatButton(
child: Text('Dismiss'), onPressed: () => Navigator.pop(context),),
],
);
});
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _auth.onAuthStateChanged,
builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
if (snapshot.hasData) {
FirebaseUser user = snapshot.data;
if (user != null) {
return HomePage();
} else {
return LoginScreen();
}
}
return LoginScreen();
},
);
}
}