Skip to content

Commit

Permalink
🔧 (config) FCM server key fetch frm firebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlphamerc committed Apr 15, 2020
1 parent 74dab31 commit 777c6a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/page/homePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class _HomePageState extends State<HomePage> {
final state = Provider.of<AuthState>(context, listen: false);
chatState.databaseInit(state.userId, state.userId);
state.updateFCMToken();
chatState.getFCMServerKey();
}

Widget _body() {
Expand Down
36 changes: 29 additions & 7 deletions lib/state/chats/chatState.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:async';
import 'dart:convert';

import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter_twitter_clone/helper/enum.dart';
import 'package:http/http.dart' as http;
import 'package:firebase_database/firebase_database.dart';
Expand All @@ -18,6 +16,9 @@ class ChatState extends AppState {
List<ChatMessage> _messageList;
List<User> _chatUserList;
User _chatUser;
String serverToken = "<FCM SERVER KEY>";

/// Get FCM server key from firebase project settings
User get chatUser => _chatUser;
set setChatUser(User model) {
_chatUser = model;
Expand Down Expand Up @@ -47,7 +48,7 @@ class ChatState extends AppState {
}
}

void databaseInit(String userId, String myId) {
void databaseInit(String userId, String myId) async {
_messageList = null;
if (_channelName == null) {
getChannelName(userId, myId);
Expand All @@ -65,6 +66,28 @@ class ChatState extends AppState {
}
}

/// FCM server key is stored in firebase remote config
/// you have to save server key in firebase remote config
/// To fetch this key go to project setting in firebase
/// Click on `cloud messaging` tab
/// Copy server key from `Project credentials`
/// Now goto `Remote Congig` section in fireabse
/// Add [FcmServerKey] as paramerter key and below json in Default vslue
/// ``` json
/// {
/// "key": "FCM server key here"
/// } ```
/// For more detail visit:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
void getFCMServerKey() async {
final RemoteConfig remoteConfig = await RemoteConfig.instance;
await remoteConfig.fetch(expiration: const Duration(hours: 5));
await remoteConfig.activateFetched();
var data = remoteConfig.getString('FcmServerKey');
if (data != null) {
serverToken = jsonDecode(data)["key"];
}
}

void getUserchatList(String userId) {
try {
final databaseReference = FirebaseDatabase.instance.reference();
Expand Down Expand Up @@ -93,7 +116,7 @@ class ChatState extends AppState {
}
}

void getchatDetailAsync() {
void getchatDetailAsync() async {
try {
final databaseReference = FirebaseDatabase.instance.reference();
databaseReference
Expand Down Expand Up @@ -242,8 +265,7 @@ class ChatState extends AppState {
if (chatUser.fcmToken == null) {
return;
}
final String serverToken = "ADD FIREBASE SERVER KEY HERE";


var body = jsonEncode(<String, dynamic>{
'notification': <String, dynamic>{
'body': model.message,
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.13"
firebase_remote_config:
dependency: "direct main"
description:
name: firebase_remote_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0+3"
firebase_storage:
dependency: "direct main"
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: A new Flutter project.
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.5+7
version: 1.0.5+9

environment:
sdk: ">=2.1.0 <3.0.0"
Expand Down Expand Up @@ -44,6 +44,7 @@ dependencies:
url_launcher:
share: ^0.6.3
google_fonts: ^0.3.9
firebase_remote_config: ^0.3.0+3

# firebase_analytics:
# camera:
Expand Down

0 comments on commit 777c6a3

Please sign in to comment.