-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.dart
230 lines (207 loc) · 6.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'dart:async';
void main() {
runApp(
MaterialApp(
home: PushMessagingExample(),
),
);
}
/*
This is the main class that contains FCM init stuff needed and the how the
UI display looks like.
*/
class PushMessagingExample extends StatefulWidget {
@override
_PushMessagingExampleState createState() => _PushMessagingExampleState();
}
class _PushMessagingExampleState extends State<PushMessagingExample> {
String _homeScreenText = "Waiting for token...";
int _bottomNavBarSelectedIndex = 0;
bool _newNotification = false;
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
setState(() {
_newNotification = true;
});
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
//Needed by iOS only
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
//Getting the token from FCM
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
setState(() {
_homeScreenText = "Push Messaging token: \n\n $token";
});
print(_homeScreenText);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Push Demo'),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: _newNotification
? Stack(
children: <Widget>[
Icon(Icons.notifications),
Positioned(
right: 0,
child: Container(
padding: EdgeInsets.all(1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(15),
),
constraints: BoxConstraints(
minWidth: 13,
minHeight: 13,
),
child: Text(
'',
style: TextStyle(
color: Colors.white,
fontSize: 8,
),
textAlign: TextAlign.center,
),
),
)
],
)
: Icon(Icons.notifications),
title: Text('Notifications'),
),
],
currentIndex: _bottomNavBarSelectedIndex,
selectedItemColor: Colors.green,
onTap: _onItemTapped,
),
body: Material(
child:
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text(_homeScreenText,style: TextStyle(fontSize: 19),),
),
),
));
}
//PRIVATE METHOD TO HANDLE NAVIGATION TO SPECIFIC PAGE
void _navigateToItemDetail(Map<String, dynamic> message) {
final MessageBean item = _itemForMessage(message);
// Clear away dialogs
Navigator.popUntil(context, (Route<dynamic> route) => route is PageRoute);
if (!item.route.isCurrent) {
Navigator.push(context, item.route);
}
}
//PRIVATE METHOD TO HANDLE TAPPED EVENT ON THE BOTTOM BAR ITEM
void _onItemTapped(int index) {
setState(() {
_bottomNavBarSelectedIndex = index;
if (index == 1){
_newNotification = false ;
}
});
}
}
final Map<String, MessageBean> _items = <String, MessageBean>{};
MessageBean _itemForMessage(Map<String, dynamic> message) {
//If the message['data'] is non-null, we will return its value, else return map message object
final dynamic data = message['data'] ?? message;
final String itemId = data['id'];
final MessageBean item = _items.putIfAbsent(
itemId, () => MessageBean(itemId: itemId))
..status = data['status'];
return item;
}
//Model class to represent the message return by FCM
class MessageBean {
MessageBean({this.itemId});
final String itemId;
StreamController<MessageBean> _controller =
StreamController<MessageBean>.broadcast();
Stream<MessageBean> get onChanged => _controller.stream;
String _status;
String get status => _status;
set status(String value) {
_status = value;
_controller.add(this);
}
static final Map<String, Route<void>> routes = <String, Route<void>>{};
Route<void> get route {
final String routeName = '/detail/$itemId';
return routes.putIfAbsent(
routeName,
() => MaterialPageRoute<void>(
settings: RouteSettings(name: routeName),
builder: (BuildContext context) => DetailPage(itemId),
),
);
}
}
//Detail UI screen that will display the content of the message return from FCM
class DetailPage extends StatefulWidget {
DetailPage(this.itemId);
final String itemId;
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
MessageBean _item;
StreamSubscription<MessageBean> _subscription;
@override
void initState() {
super.initState();
_item = _items[widget.itemId];
_subscription = _item.onChanged.listen((MessageBean item) {
if (!mounted) {
_subscription.cancel();
} else {
setState(() {
_item = item;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Item ${_item.itemId}"),
),
body: Material(
child: Center(child: Text("Item status: ${_item.status}")),
),
);
}
}