Skip to content

Commit

Permalink
Improve README of Dart push fcm
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Mar 5, 2023
1 parent 2ace6b7 commit 041e889
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
22 changes: 10 additions & 12 deletions dart/send_push_notification/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Send push notification

A Dart Cloud Function that send push notification using Firebase Cloud Messaging to particular user
A Dart Cloud Function that send push notification using FCM to particular user.

* First of all make sure that you are integrated FCM into your flutter app
- https://firebase.flutter.dev/docs/messaging/overview/
* Generate needed `FIREBASE_AUTH_KEY` env variable
* Make sure that you are integrated FCM into your app (Web/Android/Apple/Flutter)
* Generate needed `FMC_SERVER_KEY` env variable

### Hot to get FCM Server Key
* Go to Messaging settings
![FCM Settings](firebase_messaging_setting.png "Firebase Messaging Settings")

* Visit your FCM console and enter your project
* Go to Project settings and switch to Cloud Messaging tab
* Enable Cloud Messaging API (Legacy)
* Copy server key or create a new one
![FCM Server Auth Key](firebase_auth_server_key.png "Firebase Server Auth Key")

_Example input 1:_

```json
{
"user_token": "some_valid_token"
"user_token": "d0MXntS8ur81...UEFGL"
}
```

Expand Down Expand Up @@ -50,9 +50,7 @@ _Example output 2:_

List of environment variables used by this cloud function:

* **FIREBASE_AUTH_KEY** - API Key for FCM

<!-- * **TINYURL_API_KEY** - API Key for TinyUrl -->
* **FMC_SERVER_KEY** - Server Key for FCM settings

## 🚀 Deployment

Expand Down Expand Up @@ -84,7 +82,7 @@ runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtim
4. Execute function:

```shell
curl http:https://localhost:3000/ -d '{"variables":{"FIREBASE_AUTH_KEY":"YOUR_FIREBASE_AUTH_KEY"},"payload":"{\"user_token\":\"USER_FCM_TOKEN\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
curl http:https://localhost:3000/ -d '{"variables":{"FMC_SERVER_KEY":"YOUR_FMC_SERVER_KEY"},"payload":"{\"user_token\":\"USER_FCM_TOKEN\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
```

## 📝 Notes
Expand Down
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions dart/send_push_notification/lib/fcm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import 'package:http/http.dart' as http;

class FCMService {
Future<bool> sendFCMToUser(
{required String firebaseAuthKey,
{required String serverKey,
required String userFCMToken,
required Map<String, dynamic> notificationData}) async {
try {
await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'key=$firebaseAuthKey'
'Authorization': 'key=$serverKey'
},
body: jsonEncode(<String, dynamic>{
'to': userFCMToken,
Expand Down
11 changes: 3 additions & 8 deletions dart/send_push_notification/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';

import 'fcm_service.dart';

void returnSuccess(final res, final message) {
Expand All @@ -17,11 +16,7 @@ void returnFailure(final res, final String message) {
}

bool checkEnvVariables(final req, final res) {
if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null ||
req.variables['APPWRITE_FUNCTION_API_KEY'] == null ||
req.variables['APPWRITE_FUNCTION_PROJECT_ID'] == null ||
req.variables['APPWRITE_FUNCTION_USER_ID'] == null ||
req.variables['FIREBASE_AUTH_KEY'] == null) {
if (req.variables['FMC_SERVER_KEY'] == null) {
returnFailure(res, "Some Environment variables are not set");
return false;
}
Expand All @@ -42,7 +37,7 @@ Future<void> start(final req, final res) async {
if (!checkEnvVariables(req, res)) {
return;
}
final String firebaseAuthKey = req.variables['FIREBASE_AUTH_KEY'];
final String serverKey = req.variables['FMC_SERVER_KEY'];

final payload = jsonDecode(req.payload == '' ? '{}' : req.payload);
if (!checkPayload(payload, res)) {
Expand All @@ -59,7 +54,7 @@ Future<void> start(final req, final res) async {
};

final isPushSent = await fcmService.sendFCMToUser(
firebaseAuthKey: firebaseAuthKey,
serverKey: serverKey,
userFCMToken: userToken,
notificationData: notificationData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import 'package:test/test.dart';


const fakeFriendDeviceToken = 'fake_friend_device_token';
const fakeFirebaseAuthKey = 'fake_firebase_auth_key';
const fakeServerKey = 'fake_server_key';

class Request {
Map<String, dynamic> headers = {};
String payload = '';
Map<String, dynamic> variables = {
'APPWRITE_FUNCTION_ENDPOINT': 'fake_url',
'APPWRITE_FUNCTION_PROJECT_ID': 'fake_project_id',
'APPWRITE_FUNCTION_API_KEY': 'fake_api_key',
'APPWRITE_FUNCTION_USER_ID': 'fake_user_id',
'FIREBASE_AUTH_KEY': fakeFirebaseAuthKey
'FMC_SERVER_KEY': fakeServerKey
};
}

Expand All @@ -41,7 +37,7 @@ class MockFCMService extends Mock implements FCMService {}
void main() {
test('call remote function with incorrect ENV variables', () async {
final req = Request();
req.variables['FIREBASE_AUTH_KEY'] = null;
req.variables['FMC_SERVER_KEY'] = null;

final res = Response();
await start(req, res);
Expand Down Expand Up @@ -70,15 +66,15 @@ void main() {
mockFCMService = MockFCMService();

when(() => mockFCMService!.sendFCMToUser(
firebaseAuthKey: fakeFirebaseAuthKey,
serverKey: fakeServerKey,
userFCMToken: fakeFriendDeviceToken,
notificationData: any(named: 'notificationData'),
)).thenAnswer((invocation) async => true);

await start(req, res);

final capturedFCMDataArgs = verify(() => mockFCMService!.sendFCMToUser(
firebaseAuthKey: fakeFirebaseAuthKey,
serverKey: fakeServerKey,
userFCMToken: captureAny(named: 'userFCMToken'),
notificationData: captureAny(named: 'notificationData'),
)).captured;
Expand Down

0 comments on commit 041e889

Please sign in to comment.