-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.dart
163 lines (149 loc) · 5 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
print(message.data);
flutterLocalNotificationsPlugin.show(
message.data.hashCode,
message.data['title'],
message.data['body'],
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
),
));
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String token;
List subscribed = [];
List topics = [
'Samsung',
'Apple',
'Huawei',
'Nokia',
'Sony',
'HTC',
'Lenovo'
];
@override
void initState() {
super.initState();
var initialzationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettings =
InitializationSettings(android: initialzationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: android?.smallIcon,
),
));
}
});
getToken();
getTopics();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('appbar'),
),
body: ListView.builder(
itemCount: topics.length,
itemBuilder: (context, index) => ListTile(
title: Text(topics[index]),
trailing: subscribed.contains(topics[index])
? ElevatedButton(
onPressed: () async {
await FirebaseMessaging.instance
.unsubscribeFromTopic(topics[index]);
await FirebaseFirestore.instance
.collection('topics')
.doc(token)
.update({topics[index]: FieldValue.delete()});
setState(() {
subscribed.remove(topics[index]);
});
},
child: Text('unsubscribe'),
)
: ElevatedButton(
onPressed: () async {
await FirebaseMessaging.instance
.subscribeToTopic(topics[index]);
await FirebaseFirestore.instance
.collection('topics')
.doc(token)
.set({topics[index]: 'subscribe'},
SetOptions(merge: true));
setState(() {
subscribed.add(topics[index]);
});
},
child: Text('subscribe')),
),
)),
);
}
getToken() async {
token = await FirebaseMessaging.instance.getToken();
setState(() {
token = token;
});
print(token);
}
getTopics() async {
await FirebaseFirestore.instance
.collection('topics')
.get()
.then((value) => value.docs.forEach((element) {
if (token == element.id) {
subscribed = element.data().keys.toList();
}
}));
setState(() {
subscribed = subscribed;
});
}
}