Skip to content

Commit 9effe94

Browse files
committed
🚧 Firebase messageing service in progress.
1 parent cd411af commit 9effe94

File tree

9 files changed

+104
-5
lines changed

9 files changed

+104
-5
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<intent-filter>
2929
<action android:name="android.intent.action.MAIN"/>
3030
<category android:name="android.intent.category.LAUNCHER"/>
31+
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
32+
<category android:name="android.intent.category.DEFAULT" />
3133
</intent-filter>
3234
</activity>
3335
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

lib/model/user.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class User {
1414
bool isVerified;
1515
int followers;
1616
int following;
17+
String fcmToken;
1718
List<String> followersList;
1819
List<String> followingList;
1920

@@ -33,6 +34,7 @@ class User {
3334
this.following,
3435
this.webSite,
3536
this.isVerified,
37+
this.fcmToken,
3638
this.followersList,
3739
});
3840

@@ -57,6 +59,7 @@ class User {
5759
following = map['following'];
5860
userName = map['userName'];
5961
webSite = map['webSite'];
62+
fcmToken = map['fcmToken'];
6063
isVerified = map['isVerified'] ?? false;
6164
if(map['followerList'] != null){
6265
followersList = List<String>();
@@ -91,6 +94,7 @@ class User {
9194
'userName': userName,
9295
'webSite': webSite,
9396
'isVerified': isVerified ?? false,
97+
'fcmToken':fcmToken,
9498
'followerList' : followersList,
9599
'followingList':followingList
96100
};
@@ -112,6 +116,7 @@ class User {
112116
int following,
113117
String webSite,
114118
bool isVerified,
119+
String fcmToken,
115120
List<String> followingList,
116121
}) {
117122
return User(
@@ -130,6 +135,7 @@ class User {
130135
userId: userId ?? this.userId,
131136
userName: userName ?? this.userName,
132137
webSite: webSite ?? this.webSite,
138+
fcmToken:fcmToken ?? this.fcmToken,
133139
followersList: followersList ?? this.followersList,
134140
);
135141
}

lib/page/homePage.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class _HomePageState extends State<HomePage> {
5656
var state = Provider.of<NotificationState>(context, listen: false);
5757
var authstate = Provider.of<AuthState>(context, listen: false);
5858
state.databaseInit(authstate.userId);
59+
state.initfirebaseService();
5960
}
6061

6162
void initChat() {

lib/page/settings/accountSettings/privacyAndSafety/directMessage/directMessage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DirectMessagesPage extends StatelessWidget {
3131
showDivider: false,
3232
visibleSwitch: true,
3333
vPadding: 20,
34-
subtitle: 'You will be able to receive Direct Message requests from anyone on Twitter, even if you don\'t follow them.',
34+
subtitle: 'You will be able to receive Direct Message requests from anyone on Fwitter, even if you don\'t follow them.',
3535
),
3636
SettingRowWidget(
3737
"Show read receipts",

lib/state/authState.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:io';
22

33
import 'package:firebase_auth/firebase_auth.dart';
44
import 'package:firebase_database/firebase_database.dart';
5+
import 'package:firebase_messaging/firebase_messaging.dart';
56
import 'package:firebase_storage/firebase_storage.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_twitter_clone/helper/enum.dart';
@@ -18,7 +19,7 @@ class AuthState extends AppState {
1819
bool isSignInWithGoogle = false;
1920
FirebaseUser user;
2021
String userId;
21-
22+
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
2223
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
2324
final GoogleSignIn _googleSignIn = GoogleSignIn();
2425
dabase.Query _profileQuery;
@@ -189,6 +190,11 @@ class AuthState extends AppState {
189190

190191
// Time at which user is created
191192
user.createdAt = DateTime.now().toUtc().toString();
193+
194+
_firebaseMessaging.getToken().then((String token) {
195+
assert(token != null);
196+
user.fcmToken = token;
197+
});
192198
}
193199
kDatabase.child('profile').child(user.userId).set(user.toJson());
194200
_userModel = user;

lib/state/chats/chatState.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
14
import 'package:firebase_analytics/firebase_analytics.dart';
5+
import 'package:http/http.dart' as http;
26
import 'package:firebase_database/firebase_database.dart';
7+
import 'package:firebase_messaging/firebase_messaging.dart';
38
import 'package:flutter_twitter_clone/model/chatModel.dart';
49
import 'package:flutter_twitter_clone/helper/utility.dart';
510
import 'package:flutter_twitter_clone/model/user.dart';
@@ -138,6 +143,7 @@ class ChatState extends AppState {
138143
.child(_channelName)
139144
.push()
140145
.set(message.toJson());
146+
sendAndRetrieveMessage(secondUser.fcmToken);
141147
logEvent('send_message');
142148
} catch (error) {
143149
cprint(error);
@@ -222,4 +228,39 @@ class ChatState extends AppState {
222228
_messageList = null;
223229
// _channelName = null;
224230
}
231+
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);
255+
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+
}
225266
}

lib/state/notificationState.dart

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dart:async';
2-
2+
import 'dart:convert';
3+
import 'package:http/http.dart' as http;
34
import 'package:firebase_database/firebase_database.dart';
5+
import 'package:firebase_messaging/firebase_messaging.dart';
46
import 'package:flutter/material.dart';
57
import 'package:flutter_twitter_clone/helper/utility.dart';
68
import 'package:firebase_database/firebase_database.dart' as dabase;
@@ -10,9 +12,12 @@ import 'package:flutter_twitter_clone/model/user.dart';
1012
import 'package:flutter_twitter_clone/state/appState.dart';
1113

1214
class NotificationState extends AppState {
15+
String fcmToken;
1316
dabase.Query query;
1417
List<User> userList = [];
1518

19+
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
20+
1621
List<NotificationModel> _notificationList;
1722

1823
List<NotificationModel> get notificationList => _notificationList;
@@ -137,4 +142,35 @@ class NotificationState extends AppState {
137142
print("Notification Removed");
138143
}
139144
}
145+
146+
void initfirebaseService(){
147+
_firebaseMessaging.configure(
148+
onMessage: (Map<String, dynamic> message) async {
149+
print("onMessage: $message");
150+
// _showItemDialog(message);
151+
},
152+
onLaunch: (Map<String, dynamic> message) async {
153+
print("onLaunch: $message");
154+
// _navigateToItemDetail(message);
155+
},
156+
onResume: (Map<String, dynamic> message) async {
157+
print("onResume: $message");
158+
// _navigateToItemDetail(message);
159+
},
160+
);
161+
_firebaseMessaging.requestNotificationPermissions(
162+
const IosNotificationSettings(
163+
sound: true, badge: true, alert: true, provisional: true));
164+
_firebaseMessaging.onIosSettingsRegistered
165+
.listen((IosNotificationSettings settings) {
166+
print("Settings registered: $settings");
167+
});
168+
_firebaseMessaging.getToken().then((String token) {
169+
assert(token != null);
170+
fcmToken = token;
171+
print(token);
172+
});
173+
}
174+
175+
140176
}

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ packages:
148148
url: "https://pub.dartlang.org"
149149
source: hosted
150150
version: "3.1.0"
151+
firebase_messaging:
152+
dependency: "direct main"
153+
description:
154+
name: firebase_messaging
155+
url: "https://pub.dartlang.org"
156+
source: hosted
157+
version: "6.0.13"
151158
firebase_storage:
152159
dependency: "direct main"
153160
description:

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies:
3838
async:
3939
http:
4040
shared_preferences: ^0.5.1+2
41-
# firebase_messaging:
41+
firebase_messaging: ^6.0.13
4242
google_sign_in:
4343
intl: ^0.15.8
4444
url_launcher:
@@ -47,7 +47,7 @@ dependencies:
4747

4848
# firebase_analytics:
4949
# camera:
50-
# image: ^2.0.7
50+
# image: ^2.0.75
5151
# path_provider: ^0.5.0+1
5252
# font_awesome_flutter: ^8.4.0
5353
# location: ^2.3.3

0 commit comments

Comments
 (0)