Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.dart
- void main() {
- SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
- runApp(MyApp());
- }
- class MyApp extends StatefulWidget {
- @override
- State<StatefulWidget> createState() => _AppState();
- }
- class _AppState extends State<MyApp> {
- final FirebaseMessaging _fm = new FirebaseMessaging();
- final GlobalKey<NavigatorState> navigatorKey =
- GlobalKey(debugLabel: 'Main Navigator');
- final routes = {
- // ...
- };
- fcmSetting(context) {
- _fm.configure(
- onLaunch: (Map<String, dynamic> message) {
- pushTo(message); // not work
- },
- onMessage: (Map<String, dynamic> message) {
- print('onMessage $message'); // it run well
- onMessageSend(message); // but it's not
- },
- onResume: (Map<String, dynamic> message) {
- pushTo(message); // not work
- },
- );
- _fm.requestNotificationPermissions(
- const IosNotificationSettings(sound: true, badge: true, alert: true));
- _fm.getToken().then((token) {
- DBFactory.getInstance().insert('fcmToken', token);
- });
- }
- pushTo(message) {
- if (message['type'] == 'notice') {
- Navigator.of(navigatorKey.currentContext).push(
- MaterialPageRoute(builder: (_) => NoticeDetailScreen(message['id'])));
- }
- }
- onMessageSend(message) {
- showDialog(
- context: context,
- builder: (bd) => new AlertDialog(
- title: LText('메세지 도착!'),
- content: LText('$message'),
- actions: <Widget>[
- LFlatButton(
- text: '확인', onPressed: () => Navigator.of(bd).pop()),
- ],
- ));
- }
- @override
- Widget build(BuildContext context) {
- fcmSetting(context);
- return new MaterialApp(
- navigatorKey: navigatorKey,
- title: 'App title',
- initialRoute: '/',
- routes: routes,
- );
- }
- }
Add Comment
Please, Sign In to add comment