-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
118 lines (102 loc) · 3.36 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
112
113
114
115
116
117
118
// ignore_for_file: avoid_print
import 'dart:convert';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:smart_led/message.dart';
import 'package:smart_led/firebase_options.dart';
import 'package:smart_led/led_screen.dart';
import 'package:smart_led/notifications.dart';
final navigatorKey = GlobalKey<NavigatorState>();
// function to listen to background changes
Future _firebaseBackgroundMessage(RemoteMessage message) async {
if (message.notification != null) {
print("Some notification Received in background...");
}
}
// to handle notification on foreground on web platform
void showNotification({required String title, required String body}) {
showDialog(
context: navigatorKey.currentContext!,
builder: (context) => AlertDialog(
title: Text(title),
content: Text(body),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Ok"))
],
),
);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// initialize firebase messaging
await PushNotifications.init();
// initialize local notifications
// dont use local notifications for web platform
if (!kIsWeb) {
await PushNotifications.localNotiInit();
}
// Listen to background notifications
FirebaseMessaging.onBackgroundMessage(_firebaseBackgroundMessage);
// on background notification tapped
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
if (message.notification != null) {
print("Background Notification Tapped");
navigatorKey.currentState!.pushNamed("/message", arguments: message);
}
});
// to handle foreground notifications
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
String payloadData = jsonEncode(message.data);
print("Got a message in foreground");
if (message.notification != null) {
if (kIsWeb) {
showNotification(
title: message.notification!.title!,
body: message.notification!.body!);
} else {
PushNotifications.showSimpleNotification(
title: message.notification!.title!,
body: message.notification!.body!,
payload: payloadData);
}
}
});
// for handling in terminated state
final RemoteMessage? message =
await FirebaseMessaging.instance.getInitialMessage();
if (message != null) {
print("Launched from terminated state");
Future.delayed(const Duration(seconds: 1), () {
navigatorKey.currentState!.pushNamed("/message", arguments: message);
});
}
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sample Flutter Firebase Notifications',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
navigatorKey: navigatorKey,
routes: {
"/": (context) => const NormalLedScreen(),
},
);
}
}