@@ -2,6 +2,7 @@ import 'dart:async';
2
2
import 'dart:convert' ;
3
3
4
4
import 'package:firebase_analytics/firebase_analytics.dart' ;
5
+ import 'package:flutter_twitter_clone/helper/enum.dart' ;
5
6
import 'package:http/http.dart' as http;
6
7
import 'package:firebase_database/firebase_database.dart' ;
7
8
import 'package:firebase_messaging/firebase_messaging.dart' ;
@@ -13,7 +14,7 @@ import 'package:flutter_twitter_clone/state/appState.dart';
13
14
class ChatState extends AppState {
14
15
bool setIsChatScreenOpen;
15
16
final FirebaseDatabase _database = FirebaseDatabase .instance;
16
-
17
+
17
18
List <ChatMessage > _messageList;
18
19
List <User > _chatUserList;
19
20
User _chatUser;
@@ -29,8 +30,9 @@ class ChatState extends AppState {
29
30
if (_messageList == null ) {
30
31
return null ;
31
32
} else {
32
- _messageList.sort ((x, y) =>
33
- DateTime .parse (x.createdAt).toLocal ().compareTo (DateTime .parse (y.createdAt).toLocal ()));
33
+ _messageList.sort ((x, y) => DateTime .parse (x.createdAt)
34
+ .toLocal ()
35
+ .compareTo (DateTime .parse (y.createdAt).toLocal ()));
34
36
_messageList.reversed;
35
37
_messageList = _messageList.reversed.toList ();
36
38
return List .from (_messageList);
@@ -143,7 +145,7 @@ class ChatState extends AppState {
143
145
.child (_channelName)
144
146
.push ()
145
147
.set (message.toJson ());
146
- sendAndRetrieveMessage (secondUser.fcmToken );
148
+ sendAndRetrieveMessage (message );
147
149
logEvent ('send_message' );
148
150
} catch (error) {
149
151
cprint (error);
@@ -229,38 +231,43 @@ class ChatState extends AppState {
229
231
// _channelName = null;
230
232
}
231
233
232
- final FirebaseMessaging firebaseMessaging = FirebaseMessaging ();
233
-
234
- void sendAndRetrieveMessage (String message) async {
235
- await firebaseMessaging.requestNotificationPermissions (
236
- const IosNotificationSettings (sound: true , badge: true , alert: true , provisional: false ),
237
- );
238
- final String serverToken = '' ;
239
- String token = await firebaseMessaging.getToken ();
240
- var body = jsonEncode (
241
- < String , dynamic > {
242
- 'notification' : < String , dynamic > {
243
- 'body' : 'this is a body' ,
244
- 'title' : 'this is a title'
245
- },
246
- 'priority' : 'high' ,
247
- 'data' : < String , dynamic > {
248
- 'click_action' : 'FLUTTER_NOTIFICATION_CLICK' ,
249
- 'id' : '1' ,
250
- 'status' : 'done'
251
- },
252
- 'to' : token
253
- });
254
- print (token);
234
+ final FirebaseMessaging firebaseMessaging = FirebaseMessaging ();
255
235
256
- var response = await http.post (
257
- 'https://fcm.googleapis.com/fcm/send' ,
258
- headers: < String , String > {
259
- 'Content-Type' : 'application/json' ,
260
- 'Authorization' : 'key=$serverToken ' ,
261
- },
262
- body: body
263
- );
264
- print (response.body.toString ());
265
- }
236
+ void sendAndRetrieveMessage (ChatMessage model) async {
237
+ /// on noti
238
+ await firebaseMessaging.requestNotificationPermissions (
239
+ const IosNotificationSettings (
240
+ sound: true , badge: true , alert: true , provisional: false ),
241
+ );
242
+ if (chatUser.fcmToken == null ) {
243
+ return ;
244
+ }
245
+ final String serverToken = "ADD FIREBASE SERVER KEY HERE" ;
246
+
247
+ var body = jsonEncode (< String , dynamic > {
248
+ 'notification' : < String , dynamic > {
249
+ 'body' : model.message,
250
+ 'title' : "Message from ${model .senderName }"
251
+ },
252
+ 'priority' : 'high' ,
253
+ 'data' : < String , dynamic > {
254
+ 'click_action' : 'FLUTTER_NOTIFICATION_CLICK' ,
255
+ 'id' : '1' ,
256
+ 'status' : 'done' ,
257
+ "type" : NotificationType .Message .toString (),
258
+ "senderId" : model.senderId,
259
+ "title" : "title" ,
260
+ "body" : model.message,
261
+ "tweetId" : ""
262
+ },
263
+ 'to' : chatUser.fcmToken
264
+ });
265
+ var response = await http.post ('https://fcm.googleapis.com/fcm/send' ,
266
+ headers: < String , String > {
267
+ 'Content-Type' : 'application/json' ,
268
+ 'Authorization' : 'key=$serverToken ' ,
269
+ },
270
+ body: body);
271
+ print (response.body.toString ());
272
+ }
266
273
}
0 commit comments