Created
March 31, 2020 02:14
-
-
Save hmmhmmhm/948daaf856ecf490499acade6eced18a to your computer and use it in GitHub Desktop.
firebase-push-message-send-node-js-example.ts
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
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