-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.dart
94 lines (85 loc) · 2.54 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
import 'package:djangoflow_fcm/djangoflow_fcm.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
// import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
// TODO add firebase options to test
// options: DefaultFirebaseOptions.currentPlatform,
);
runApp(
MultiBlocProvider(
providers: [
BlocProvider<DjangoflowFCMBloc>(
create: (context) => DjangoflowFCMBloc(
DjangoflowFCMRepository(FirebaseMessaging.instance),
),
),
],
child: const MainApp(),
),
);
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: _HomePage(),
);
}
}
class _HomePage extends StatelessWidget {
const _HomePage();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Djangoflow FCM Example'),
),
body: BlocConsumer<DjangoflowFCMBloc, DjangoflowFCMState>(
listener: (context, state) {
final notification = state.remoteMessage?.notification;
if (notification != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
<String>[
if (notification.title != null) notification.title!,
if (notification.body != null)
notification.body!
else
'Message Received',
].join('\n'),
),
),
);
}
},
builder: (context, state) {
final fcmToken = state.token;
return ListView(
padding: const EdgeInsets.all(16),
children: [
if (fcmToken == null) ...[
const Text('Token is null'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => context.read<DjangoflowFCMBloc>().add(
DjangoflowFCMTokenRequested(),
),
child: const Text('Request FCM Token'),
),
] else
Text(fcmToken),
const SizedBox(height: 16),
],
);
},
),
);
}
}