Skip to content

Commit b1d9f33

Browse files
committed
✨ Open tweet detail when clicked on push notification if user tag available.
⬆️ update packages.
1 parent d4ec2b4 commit b1d9f33

20 files changed

+274
-380
lines changed

ios/Flutter/Flutter.podspec

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/helper/utility.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import '../widgets/newWidget/customLoader.dart';
1616
final kAnalytics = FirebaseAnalytics();
1717
final DatabaseReference kDatabase = FirebaseDatabase.instance.reference();
1818
final kScreenloader = CustomLoader();
19-
void cprint(dynamic data, {String errorIn, String event}) {
19+
void cprint(dynamic data,
20+
{String errorIn, String event, String label = 'Log'}) {
2021
/// Print logs only in development mode
2122
if (kDebugMode) {
2223
if (errorIn != null) {
@@ -27,7 +28,7 @@ void cprint(dynamic data, {String errorIn, String event}) {
2728
print(
2829
'****************************** error ******************************');
2930
} else if (data != null) {
30-
developer.log(data, time: DateTime.now());
31+
developer.log(data, time: DateTime.now(), name: label);
3132
}
3233
if (event != null) {
3334
Utility.logEvent(event);

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MyApp extends StatelessWidget {
3636
child: MaterialApp(
3737
title: 'Fwitter',
3838
theme: AppTheme.apptheme.copyWith(
39-
textTheme: GoogleFonts.muliTextTheme(
39+
textTheme: GoogleFonts.mulishTextTheme(
4040
Theme.of(context).textTheme,
4141
),
4242
),

lib/model/push_notification_model.dart

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,6 @@ import 'dart:convert';
22

33
class PushNotificationModel {
44
PushNotificationModel({
5-
this.notification,
6-
this.data,
7-
});
8-
9-
final Notification notification;
10-
final Data data;
11-
12-
factory PushNotificationModel.fromRawJson(String str) =>
13-
PushNotificationModel.fromJson(json.decode(str));
14-
15-
String toRawJson() => json.encode(toJson());
16-
17-
factory PushNotificationModel.fromJson(Map<dynamic, dynamic> json) =>
18-
PushNotificationModel(
19-
notification: Notification.fromJson(json["notification"]),
20-
data: Data.fromJson(json["data"]),
21-
);
22-
23-
Map<String, dynamic> toJson() => {
24-
"notification": notification.toJson(),
25-
"data": data.toJson(),
26-
};
27-
}
28-
29-
class Data {
30-
Data({
315
this.id,
326
this.type,
337
this.receiverId,
@@ -45,11 +19,13 @@ class Data {
4519
final String body;
4620
final String tweetId;
4721

48-
factory Data.fromRawJson(String str) => Data.fromJson(json.decode(str));
22+
factory PushNotificationModel.fromRawJson(String str) =>
23+
PushNotificationModel.fromJson(json.decode(str));
4924

5025
String toRawJson() => json.encode(toJson());
5126

52-
factory Data.fromJson(Map<dynamic, dynamic> json) => Data(
27+
factory PushNotificationModel.fromJson(Map<dynamic, dynamic> json) =>
28+
PushNotificationModel(
5329
id: json["id"],
5430
type: json["type"],
5531
receiverId: json["receiverId"],

lib/resource/push_notification_service.dart

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import 'dart:async';
2+
13
import 'package:firebase_messaging/firebase_messaging.dart';
24
import 'package:flutter_twitter_clone/helper/utility.dart';
35
import 'package:flutter_twitter_clone/model/push_notification_model.dart';
46
import 'package:rxdart/rxdart.dart';
57

8+
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
9+
// If you're going to use other Firebase services in the background, such as Firestore,
10+
// make sure you call `initializeApp` before using other Firebase services.
11+
cprint("Handling a background message: ${message.messageId}");
12+
}
13+
614
class PushNotificationService {
715
final FirebaseMessaging _firebaseMessaging;
816

@@ -15,23 +23,54 @@ class PushNotificationService {
1523
Stream<PushNotificationModel> get pushNotificationResponseStream =>
1624
_pushNotificationSubject.stream;
1725

26+
StreamSubscription<RemoteMessage> _backgroundMessageSubscription;
27+
1828
void initializeMessages() {
19-
_firebaseMessaging.requestNotificationPermissions(
20-
const IosNotificationSettings(sound: true, badge: true, alert: true));
29+
// _firebaseMessaging.requestNotificationPermissions(
30+
// const IosNotificationSettings(sound: true, badge: true, alert: true));
2131
configure();
2232
}
2333

24-
/// Configure the firebase messaging handler
25-
void configure() {
26-
_firebaseMessaging.configure(
27-
onMessage: (Map<String, dynamic> message) async {
28-
myBackgroundMessageHandler(message, onMessage: true);
29-
}, onLaunch: (Map<String, dynamic> message) async {
30-
myBackgroundMessageHandler(message, onLaunch: true);
31-
}, onResume: (Map<String, dynamic> message) async {
32-
myBackgroundMessageHandler(message, onResume: true);
33-
});
34+
/// Configured from Home page
35+
void configure() async {
3436
_pushNotificationSubject = PublishSubject<PushNotificationModel>();
37+
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
38+
cprint('Got a message whilst in the foreground!');
39+
40+
try {
41+
// var data = json.decode(message.data.toString()) as Map<String, dynamic>;
42+
myBackgroundMessageHandler(message.data, onMessage: true);
43+
} catch (e) {
44+
cprint(e, errorIn: "On Message");
45+
}
46+
});
47+
48+
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
49+
50+
/// Get message when the app is in the Terminated form
51+
FirebaseMessaging.instance.getInitialMessage().then((event) {
52+
if (event != null && event.data != null) {
53+
try {
54+
myBackgroundMessageHandler(event.data, onLaunch: true);
55+
} catch (e) {
56+
cprint(e, errorIn: "On getInitialMessage");
57+
}
58+
}
59+
});
60+
61+
/// Returns a [Stream] that is called when a user presses a notification message displayed via FCM.
62+
_backgroundMessageSubscription =
63+
FirebaseMessaging.onMessageOpenedApp.listen((event) {
64+
if (event != null && event.data != null) {
65+
if (event != null && event.data != null) {
66+
try {
67+
myBackgroundMessageHandler(event.data, onLaunch: true);
68+
} catch (e) {
69+
cprint(e, errorIn: "On onMessageOpenedApp");
70+
}
71+
}
72+
}
73+
});
3574
}
3675

3776
/// Return FCM token
@@ -41,15 +80,13 @@ class PushNotificationService {
4180
}
4281

4382
/// Callback triger everytime a push notification is received
44-
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message,
83+
void myBackgroundMessageHandler(Map<String, dynamic> message,
4584
{bool onBackGround = false,
4685
bool onLaunch = false,
4786
bool onMessage = false,
4887
bool onResume = false}) async {
4988
try {
50-
if (!onMessage &&
51-
message["data"] != null &&
52-
message["notification"] != null) {
89+
if (!onMessage) {
5390
PushNotificationModel model = PushNotificationModel.fromJson(message);
5491
_pushNotificationSubject.add(model);
5592
}

lib/state/authState.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AuthState extends AppState {
2222
bool isSignInWithGoogle = false;
2323
User user;
2424
String userId;
25-
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
25+
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
2626
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
2727
final GoogleSignIn _googleSignIn = GoogleSignIn();
2828
final FirebaseStorage _firebaseStorage = FirebaseStorage.instance;

lib/state/chats/chatState.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:flutter_twitter_clone/state/appState.dart';
1111

1212
class ChatState extends AppState {
1313
bool setIsChatScreenOpen;
14-
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
14+
final FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
1515

1616
List<ChatMessage> _messageList;
1717
List<ChatMessage> _chatUserList;
@@ -83,8 +83,8 @@ class ChatState extends AppState {
8383
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
8484
void getFCMServerKey() async {
8585
final RemoteConfig remoteConfig = await RemoteConfig.instance;
86-
await remoteConfig.fetch(expiration: const Duration(days: 5));
87-
await remoteConfig.activateFetched();
86+
await remoteConfig.fetchAndActivate();
87+
// await remoteConfig.
8888
var data = remoteConfig.getString('FcmServerKey');
8989
if (data != null && data.isNotEmpty) {
9090
serverToken = jsonDecode(data)["key"];
@@ -284,10 +284,10 @@ class ChatState extends AppState {
284284
/// Push notification will be sent to other user when you send him a message in chat.
285285
/// To send push notification make sure you have FCM `serverToken`
286286
void sendAndRetrieveMessage(ChatMessage model) async {
287-
await firebaseMessaging.requestNotificationPermissions(
288-
const IosNotificationSettings(
289-
sound: true, badge: true, alert: true, provisional: false),
290-
);
287+
// await firebaseMessaging.requestNotificationPermissions(
288+
// const IosNotificationSettings(
289+
// sound: true, badge: true, alert: true, provisional: false),
290+
// );
291291
if (chatUser.fcmToken == null) {
292292
return;
293293
}
@@ -310,16 +310,20 @@ class ChatState extends AppState {
310310
},
311311
'to': chatUser.fcmToken
312312
});
313-
var response = await http.post('https://fcm.googleapis.com/fcm/send',
314-
headers: <String, String>{
315-
'Content-Type': 'application/json',
316-
'Authorization': 'key=$serverToken',
317-
},
318-
body: body);
313+
var response =
314+
await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
315+
headers: <String, String>{
316+
'Content-Type': 'application/json',
317+
'Authorization': 'key=$serverToken',
318+
},
319+
body: body);
319320
if (response.reasonPhrase.contains("INVALID_KEY")) {
320-
cprint("You are using Invalid FCM key");
321+
cprint(
322+
"You are using Invalid FCM key",
323+
errorIn: "sendAndRetrieveMessage",
324+
);
321325
return;
322326
}
323-
print(response.body.toString());
327+
cprint(response.body.toString());
324328
}
325329
}

lib/state/feedState.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,10 @@ class FeedState extends AppState {
286286
notifyListeners();
287287
String tweetKey;
288288
try {
289-
DatabaseReference dbReference = kDatabase.child('tweet');
289+
DatabaseReference dbReference = kDatabase.child('tweet').push();
290+
291+
await dbReference.set(model.toJson());
290292

291-
await dbReference.push().set(model.toJson());
292293
tweetKey = dbReference.key;
293294
} catch (error) {
294295
cprint(error, errorIn: 'createTweet');

lib/state/notificationState.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class NotificationState extends AppState {
168168
void initfirebaseService() {
169169
if (!getIt.isRegistered<PushNotificationService>()) {
170170
getIt.registerSingleton<PushNotificationService>(
171-
PushNotificationService(FirebaseMessaging()));
171+
PushNotificationService(FirebaseMessaging.instance));
172172
}
173173
}
174174

lib/ui/page/common/splash.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class _SplashPageState extends State<SplashPage> {
9191
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
9292
Future<String> _getAppVersionFromFirebaseConfig() async {
9393
final RemoteConfig remoteConfig = await RemoteConfig.instance;
94-
await remoteConfig.fetch(expiration: const Duration(minutes: 1));
95-
await remoteConfig.activateFetched();
94+
await remoteConfig.fetchAndActivate();
95+
// await remoteConfig.activateFetched();
9696
var data = remoteConfig.getString('appVersion');
9797
if (data != null && data.isNotEmpty) {
9898
return jsonDecode(data)["key"];

0 commit comments

Comments
 (0)