@@ -7,7 +7,10 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
7
7
8
8
var localMessaging = new FlutterLocalNotificationsPlugin ();
9
9
10
- void main () => runApp (App ());
10
+ void main () {
11
+ runApp (App ());
12
+ configureMessaging ();
13
+ }
11
14
12
15
class App extends StatelessWidget {
13
16
@override
@@ -25,8 +28,6 @@ class HomePage extends StatefulWidget {
25
28
}
26
29
27
30
class _HomePageState extends State <HomePage > {
28
- final FirebaseMessaging messaging = FirebaseMessaging ();
29
-
30
31
@override
31
32
void initState () {
32
33
SystemChrome .setSystemUIOverlayStyle (SystemUiOverlayStyle (
@@ -37,45 +38,44 @@ class _HomePageState extends State<HomePage> {
37
38
));
38
39
39
40
super .initState ();
40
- _configureMessaging ();
41
41
}
42
42
43
43
@override
44
44
Widget build (BuildContext context) {
45
45
return Scaffold ();
46
46
}
47
+ }
47
48
48
- void _configureMessaging () {
49
- var android =
50
- new AndroidInitializationSettings ('@drawable/ic_notification' );
51
- var iOS = new IOSInitializationSettings ();
52
- var settings = new InitializationSettings (android, iOS);
53
-
54
- localMessaging.initialize (
55
- settings,
56
- onSelectNotification: _onSelectNotification,
57
- );
49
+ void configureMessaging () {
50
+ var android = new AndroidInitializationSettings ('@drawable/ic_notification' );
51
+ var iOS = IOSInitializationSettings ();
52
+ var settings = InitializationSettings (android, iOS);
58
53
59
- messaging.getToken ().then ((String token) async {
60
- print ("FCM Token : $token " );
61
- });
54
+ localMessaging.initialize (
55
+ settings,
56
+ onSelectNotification: _onSelectNotification,
57
+ );
62
58
63
- messaging.configure (
64
- onMessage: (Map <String , dynamic > message) async {
65
- print ("onMessage: $message " );
66
- },
67
- onBackgroundMessage: Platform .isIOS ? null : backgroundMessageHandler,
68
- onResume: (Map <String , dynamic > message) async {
69
- print ("onResume: $message " );
70
- },
71
- onLaunch: (Map <String , dynamic > message) async {
72
- print ("onLaunch: $message " );
73
- });
74
- }
59
+ final FirebaseMessaging messaging = FirebaseMessaging ();
60
+ messaging.getToken ().then ((String token) async {
61
+ print ("FCM Token : $token " );
62
+ });
63
+
64
+ messaging.configure (
65
+ onMessage: (Map <String , dynamic > message) async {
66
+ print ("onMessage: $message " );
67
+ },
68
+ onBackgroundMessage: Platform .isIOS ? null : backgroundMessageHandler,
69
+ onResume: (Map <String , dynamic > message) async {
70
+ print ("onResume: $message " );
71
+ },
72
+ onLaunch: (Map <String , dynamic > message) async {
73
+ print ("onLaunch: $message " );
74
+ });
75
+ }
75
76
76
- Future _onSelectNotification (String data) async {
77
- print ("onSelectNotification: $data " );
78
- }
77
+ Future _onSelectNotification (String data) async {
78
+ print ("onSelectNotification: $data " );
79
79
}
80
80
81
81
Future backgroundMessageHandler (Map <String , dynamic > message) async {
@@ -89,11 +89,12 @@ Future backgroundMessageHandler(Map<String, dynamic> message) async {
89
89
var iOS = new IOSNotificationDetails ();
90
90
var notificationDetails = NotificationDetails (android, iOS);
91
91
92
+ int id = int .tryParse (message["data" ]["id" ].toString ()) ?? 1 ;
92
93
localMessaging.show (
93
- 1 ,
94
+ id ,
94
95
message["data" ]["title" ],
95
96
message["data" ]["body" ],
96
97
notificationDetails,
97
- payload: message["data" ]["payload " ],
98
+ payload: message["data" ]["data " ],
98
99
);
99
100
}
0 commit comments