Skip to content

Commit 777c6a3

Browse files
committed
🔧 (config) FCM server key fetch frm firebase.
1 parent 74dab31 commit 777c6a3

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

lib/page/homePage.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class _HomePageState extends State<HomePage> {
6666
final state = Provider.of<AuthState>(context, listen: false);
6767
chatState.databaseInit(state.userId, state.userId);
6868
state.updateFCMToken();
69+
chatState.getFCMServerKey();
6970
}
7071

7172
Widget _body() {

lib/state/chats/chatState.dart

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import 'dart:async';
21
import 'dart:convert';
3-
4-
import 'package:firebase_analytics/firebase_analytics.dart';
2+
import 'package:firebase_remote_config/firebase_remote_config.dart';
53
import 'package:flutter_twitter_clone/helper/enum.dart';
64
import 'package:http/http.dart' as http;
75
import 'package:firebase_database/firebase_database.dart';
@@ -18,6 +16,9 @@ class ChatState extends AppState {
1816
List<ChatMessage> _messageList;
1917
List<User> _chatUserList;
2018
User _chatUser;
19+
String serverToken = "<FCM SERVER KEY>";
20+
21+
/// Get FCM server key from firebase project settings
2122
User get chatUser => _chatUser;
2223
set setChatUser(User model) {
2324
_chatUser = model;
@@ -47,7 +48,7 @@ class ChatState extends AppState {
4748
}
4849
}
4950

50-
void databaseInit(String userId, String myId) {
51+
void databaseInit(String userId, String myId) async {
5152
_messageList = null;
5253
if (_channelName == null) {
5354
getChannelName(userId, myId);
@@ -65,6 +66,28 @@ class ChatState extends AppState {
6566
}
6667
}
6768

69+
/// FCM server key is stored in firebase remote config
70+
/// you have to save server key in firebase remote config
71+
/// To fetch this key go to project setting in firebase
72+
/// Click on `cloud messaging` tab
73+
/// Copy server key from `Project credentials`
74+
/// Now goto `Remote Congig` section in fireabse
75+
/// Add [FcmServerKey] as paramerter key and below json in Default vslue
76+
/// ``` json
77+
/// {
78+
/// "key": "FCM server key here"
79+
/// } ```
80+
/// For more detail visit:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
81+
void getFCMServerKey() async {
82+
final RemoteConfig remoteConfig = await RemoteConfig.instance;
83+
await remoteConfig.fetch(expiration: const Duration(hours: 5));
84+
await remoteConfig.activateFetched();
85+
var data = remoteConfig.getString('FcmServerKey');
86+
if (data != null) {
87+
serverToken = jsonDecode(data)["key"];
88+
}
89+
}
90+
6891
void getUserchatList(String userId) {
6992
try {
7093
final databaseReference = FirebaseDatabase.instance.reference();
@@ -93,7 +116,7 @@ class ChatState extends AppState {
93116
}
94117
}
95118

96-
void getchatDetailAsync() {
119+
void getchatDetailAsync() async {
97120
try {
98121
final databaseReference = FirebaseDatabase.instance.reference();
99122
databaseReference
@@ -242,8 +265,7 @@ class ChatState extends AppState {
242265
if (chatUser.fcmToken == null) {
243266
return;
244267
}
245-
final String serverToken = "ADD FIREBASE SERVER KEY HERE";
246-
268+
247269
var body = jsonEncode(<String, dynamic>{
248270
'notification': <String, dynamic>{
249271
'body': model.message,

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ packages:
155155
url: "https://pub.dartlang.org"
156156
source: hosted
157157
version: "6.0.13"
158+
firebase_remote_config:
159+
dependency: "direct main"
160+
description:
161+
name: firebase_remote_config
162+
url: "https://pub.dartlang.org"
163+
source: hosted
164+
version: "0.3.0+3"
158165
firebase_storage:
159166
dependency: "direct main"
160167
description:

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: A new Flutter project.
1111
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
1212
# Read more about iOS versioning at
1313
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
14-
version: 1.0.5+7
14+
version: 1.0.5+9
1515

1616
environment:
1717
sdk: ">=2.1.0 <3.0.0"
@@ -44,6 +44,7 @@ dependencies:
4444
url_launcher:
4545
share: ^0.6.3
4646
google_fonts: ^0.3.9
47+
firebase_remote_config: ^0.3.0+3
4748

4849
# firebase_analytics:
4950
# camera:

0 commit comments

Comments
 (0)