Skip to content

Commit 81825b3

Browse files
committed
Make all configuration to top level function
1 parent 2dc440a commit 81825b3

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
<action android:name="android.intent.action.MAIN" />
2727
<category android:name="android.intent.category.LAUNCHER" />
2828
</intent-filter>
29-
30-
<intent-filter>
31-
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
32-
<category android:name="android.intent.category.DEFAULT" />
33-
</intent-filter>
3429
</activity>
3530

3631
<meta-data

lib/main.dart

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
77

88
var localMessaging = new FlutterLocalNotificationsPlugin();
99

10-
void main() => runApp(App());
10+
void main() {
11+
runApp(App());
12+
configureMessaging();
13+
}
1114

1215
class App extends StatelessWidget {
1316
@override
@@ -25,8 +28,6 @@ class HomePage extends StatefulWidget {
2528
}
2629

2730
class _HomePageState extends State<HomePage> {
28-
final FirebaseMessaging messaging = FirebaseMessaging();
29-
3031
@override
3132
void initState() {
3233
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
@@ -37,45 +38,44 @@ class _HomePageState extends State<HomePage> {
3738
));
3839

3940
super.initState();
40-
_configureMessaging();
4141
}
4242

4343
@override
4444
Widget build(BuildContext context) {
4545
return Scaffold();
4646
}
47+
}
4748

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);
5853

59-
messaging.getToken().then((String token) async {
60-
print("FCM Token : $token");
61-
});
54+
localMessaging.initialize(
55+
settings,
56+
onSelectNotification: _onSelectNotification,
57+
);
6258

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+
}
7576

76-
Future _onSelectNotification(String data) async {
77-
print("onSelectNotification: $data");
78-
}
77+
Future _onSelectNotification(String data) async {
78+
print("onSelectNotification: $data");
7979
}
8080

8181
Future backgroundMessageHandler(Map<String, dynamic> message) async {
@@ -89,11 +89,12 @@ Future backgroundMessageHandler(Map<String, dynamic> message) async {
8989
var iOS = new IOSNotificationDetails();
9090
var notificationDetails = NotificationDetails(android, iOS);
9191

92+
int id = int.tryParse(message["data"]["id"].toString()) ?? 1;
9293
localMessaging.show(
93-
1,
94+
id,
9495
message["data"]["title"],
9596
message["data"]["body"],
9697
notificationDetails,
97-
payload: message["data"]["payload"],
98+
payload: message["data"]["data"],
9899
);
99100
}

0 commit comments

Comments
 (0)