Skip to content

Commit

Permalink
Merge pull request #282 from Snehasis4321/main
Browse files Browse the repository at this point in the history
fix bug 🐛 push notifications not working for node
  • Loading branch information
loks0n authored May 28, 2024
2 parents 55fca1c + 4cfe06e commit 882f67b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
22 changes: 21 additions & 1 deletion node/push-notification-with-fcm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,27 @@ Send a push notification to a user.
| ------------ | ------------------------------------ | -------- | ------------------ | -------------- |
| Content-Type | The content type of the request body | Header | `application/json` | N/A |
| deviceToken | FCM device identifier | Body | String | `642...7cd` |
| message | Message to send | Body | String | `Hello World!` |
| message | Message to send | Body | Object | `{"title": "hello","body": "how are you?"}` |
| data | Additional data to pass | Body | Object | `{"greet": "welcome"}` |

**Request**

`deviceToken` and `message` are required. `data` is optional.


```json
{
"deviceToken": "642...7cd",
"message": {
"title": "hello",
"body": "how are you?"
},
"data": {
"greet": "welcome"
}
}
```


**Response**

Expand Down
18 changes: 10 additions & 8 deletions node/push-notification-with-fcm/src/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { throwIfMissing, sendPushNotification } from './utils.js';

export default async ({ req, res, log, error }) => {
throwIfMissing(process.env, [
'FCM_PROJECT_ID',
'FCM_PRIVATE_KEY',
'FCM_CLIENT_EMAIL',
'FCM_DATABASE_URL',
]);
throwIfMissing(process.env, [
'FCM_PROJECT_ID',
'FCM_PRIVATE_KEY',
'FCM_CLIENT_EMAIL',
'FCM_DATABASE_URL',
]);

export default async ({ req, res, log, error }) => {
try {
throwIfMissing(req.body, ['deviceToken', 'message']);
throwIfMissing(req.body.message, ['title', 'body']);
Expand All @@ -23,8 +23,10 @@ export default async ({ req, res, log, error }) => {
title: req.body.message.title,
body: req.body.message.body,
},
token: req.deviceToken,
data: req.body.data ?? {},
token: req.body.deviceToken,
});

log(`Successfully sent message: ${response}`);

return res.json({ ok: true, messageId: response });
Expand Down
33 changes: 23 additions & 10 deletions node/push-notification-with-fcm/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import * as admin from 'firebase-admin';
import admin from 'firebase-admin';

throwIfMissing(process.env, [
'FCM_PROJECT_ID',
'FCM_PRIVATE_KEY',
'FCM_CLIENT_EMAIL',
'FCM_DATABASE_URL',
]);

// initailze firebase app
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FCM_PROJECT_ID,
clientEmail: process.env.FCM_CLIENT_EMAIL,
privateKey: process.env.FCM_PRIVATE_KEY,
}),
databaseURL: process.env.FCM_DATABASE_URL,
});

/**
* Throws an error if any of the keys are missing from the object
Expand All @@ -23,13 +40,9 @@ export function throwIfMissing(obj, keys) {
* @returns {Promise<string>}
*/
export async function sendPushNotification(payload) {
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FCM_PROJECT_ID,
clientEmail: process.env.FCM_CLIENT_EMAIL,
privateKey: process.env.FCM_PRIVATE_KEY,
}),
databaseURL: process.env.FCM_DATABASE_URL,
});
return await admin.messaging().send(payload);
try {
return await admin.messaging().send(payload);
} catch (e) {
throw "error on messaging ";
}
}

0 comments on commit 882f67b

Please sign in to comment.