Created
March 4, 2025 23:18
-
-
Save SametSahin10/6c01b3daef934ef3b10fb893208567ab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import axios from 'axios'; | |
/** | |
* Sends an FCM message using the HTTP v1 API | |
* @param serviceAccountPath Path to your service account JSON file | |
* @param projectId Your Firebase project ID | |
* @param token The FCM registration token of the target device | |
* @param title The notification title | |
* @param body The notification body | |
* @param data Optional data payload | |
* @returns Promise that resolves with the response | |
*/ | |
export async function sendFCMMessage( | |
serviceAccountPath: string, | |
projectId: string, | |
token: string, | |
title: string, | |
body: string, | |
data?: Record<string, string> | |
): Promise<any> { | |
const url = `https://fcm.googleapis.com/v1/projects/${projectId}/messages:send`; | |
// Get access token from service account | |
const accessToken = await getAccessToken(serviceAccountPath); | |
const message = { | |
message: { | |
token, | |
notification: { | |
title, | |
body, | |
}, | |
data, | |
} | |
}; | |
try { | |
const response = await axios.post( | |
url, | |
message, | |
{ | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${accessToken}` | |
} | |
} | |
); | |
console.log('Successfully sent message:', response.data); | |
return response.data; | |
} catch (error) { | |
console.error('Error sending message:', error); | |
throw error; | |
} | |
} | |
/** | |
* Gets an access token for FCM API calls | |
* @param serviceAccountPath Path to your service account JSON file | |
* @returns Promise that resolves with the access token | |
*/ | |
async function getAccessToken(serviceAccountPath: string): Promise<string> { | |
// You'll need to implement this function using the Google Auth library | |
// This is just a placeholder | |
// Install: npm install googleapis | |
const { GoogleAuth } = require('google-auth-library'); | |
const auth = new GoogleAuth({ | |
keyFile: serviceAccountPath, | |
scopes: ['https://www.googleapis.com/auth/firebase.messaging'], | |
}); | |
const accessToken = await auth.getAccessToken(); | |
return accessToken; | |
} | |
/** | |
* Sends an FCM message to a topic using the HTTP v1 API | |
* @param serviceAccountPath Path to your service account JSON file | |
* @param projectId Your Firebase project ID | |
* @param topic The FCM topic to send to (without the /topics/ prefix) | |
* @param title The notification title | |
* @param body The notification body | |
* @param data Optional data payload | |
* @returns Promise that resolves with the response | |
*/ | |
async function sendFCMMessageToTopic( | |
serviceAccountPath: string, | |
projectId: string, | |
topic: string, | |
data?: Record<string, string> | |
): Promise<any> { | |
const url = `https://fcm.googleapis.com/v1/projects/${projectId}/messages:send`; | |
// Get access token from service account | |
const accessToken = await getAccessToken(serviceAccountPath); | |
const message = { | |
message: { | |
topic, | |
data, | |
} | |
}; | |
try { | |
const response = await axios.post( | |
url, | |
message, | |
{ | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${accessToken}` | |
} | |
} | |
); | |
console.log('Successfully sent message to topic:', response.data); | |
return response.data; | |
} catch (error) { | |
console.error('Error sending message to topic:', error); | |
throw error; | |
} | |
} | |
async function main() { | |
const serviceAccountPath = ''; | |
const projectId = ''; | |
const topic = ''; | |
const data = { mail: '', number: '' }; | |
await sendFCMMessageToTopic(serviceAccountPath, projectId, topic, data); | |
console.log('Message sent successfully'); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment