Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hmmhmmhm/948daaf856ecf490499acade6eced18a to your computer and use it in GitHub Desktop.
Save hmmhmmhm/948daaf856ecf490499acade6eced18a to your computer and use it in GitHub Desktop.
firebase-push-message-send-node-js-example.ts
import * as FirebaseAdmin from 'firebase-admin'
import * as serviceAccount from '../~~.json'
FirebaseAdmin.initializeApp({
// @ts-ignore
credential: FirebaseAdmin.credential.cert(serviceAccount),
})
export const sendPushMessage = async (
notification: {
/**
* The title of the notification.
*/
title?: string
/**
* The notification body
*/
body?: string
/**
* URL of an image to be displayed in the notification.
*/
imageUrl?: string
},
registrationTokens: string[]
) => {
try {
const msg: FirebaseAdmin.messaging.MulticastMessage = {
notification,
tokens: registrationTokens,
}
const response = await FirebaseAdmin.messaging().sendMulticast(msg)
return response.successCount
} catch (e) {
console.error(e)
return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment