-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://www.loom.com/share/00f815219f1b446aa25fa3be9a57d960 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Integrated Firebase push notifications, providing an additional method to send notifications. - Added a new route for handling Firebase-related requests. - Introduced new middleware for validating requests to the Firebase endpoint. - Implemented functionality for sending push notifications using the Firebase Admin SDK. - Added support for additional parameters in token registration and user token retrieval. - Added a new entry to the `.prettierignore` file to specify ignored paths for formatting. - Created a new `.prettierrc` configuration file for code formatting options. - **Refactor** - Enhanced asynchronous processing and refined error handling for improved reliability. - Streamlined internal logging and parameter management. - **Chores** - Updated configurations and dependencies to support the new features and improvements. - Added `pnpm-lock.yaml` to `.gitignore`. - Added new columns to the `push_tokens` table in the database schema. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
17 changed files
with
384 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ jspm_packages/ | |
|
||
# Temporary | ||
package-lock.json | ||
pnpm-lock.yaml | ||
data/ | ||
|
||
# MacOS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.git | ||
.github | ||
node_modules | ||
chk-sig | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"useTabs": false, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "none", | ||
"bracketSpacing": true, | ||
"arrowParens": "always", | ||
"endOfLine": "lf" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const admin = require('firebase-admin'); | ||
const debug = require('debug')('aurora_push:routes:send'); | ||
|
||
// Firebase Admin SDK Initialization | ||
const firebaseConfig = { | ||
type: 'service_account', | ||
auth_uri: 'https://accounts.google.com/o/oauth2/auth', | ||
token_uri: 'https://oauth2.googleapis.com/token', | ||
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs', | ||
project_id: process.env.FIREBASE_PROJECT_ID, | ||
private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID, | ||
private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'), // Handle multiline env variables | ||
client_email: process.env.FIREBASE_CLIENT_EMAIL, | ||
client_id: process.env.FIREBASE_CLIENT_ID, | ||
client_x509_cert_url: process.env.FIREBASE_CLIENT_X509_CERT_URL | ||
}; | ||
|
||
// Initialize Firebase Admin SDK only once | ||
if (!admin.apps.length) { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(firebaseConfig) | ||
}); | ||
} | ||
|
||
const sendPushNotification = async (deviceToken, payload) => { | ||
const { title, pushType = 'alert', sound = 'default', body, expiry, topic } = payload; | ||
|
||
let message = { | ||
notification: { title, body }, | ||
data: {}, | ||
android: { notification: { sound } }, | ||
apns: { | ||
headers: { | ||
'apns-expiration': expiry ? String(expiry) : undefined, | ||
'apns-push-type': pushType | ||
}, | ||
payload: { | ||
aps: { | ||
alert: { title, body }, | ||
sound, | ||
mutableContent: 1 | ||
} | ||
} | ||
} | ||
}; | ||
|
||
if (topic) { | ||
message.topic = topic; | ||
} else if (deviceToken) { | ||
message.token = deviceToken; | ||
} else { | ||
throw new Error('You must provide either a deviceToken or a topic.'); | ||
} | ||
|
||
try { | ||
const response = await admin.messaging().send(message); | ||
debug(`Notification sent successfully: ${response}`); | ||
return response; | ||
} catch (error) { | ||
debug(`Error sending notification: ${error.message}`); | ||
throw error; | ||
} | ||
}; | ||
|
||
module.exports = { sendPushNotification }; |
Oops, something went wrong.