The Firebase Cloud Messaging quickstart demonstrates how to:
config.ts
file in the messaging directory.config.ts
file replace <YOUR_PUBLIC_VAPID_KEY_HERE>
with your key.npm install -g firebase-tools
and then configure it with firebase login
.firebase use --add
and select the Firebase project you have created.To run the sample app locally during development:
npm install
to install dependencies.firebase emulators:start
to start the local Firebase emulators. Note: phone authentication required ReCaptcha verification which does not work with the Firebase emulators. These examples skip connecting to the emulators.npm run dev
to serve the app locally using Vite
This will start a server locally that serves index.html
on http://localhost:5173/index.html
.YOUR-SERVER-KEY
and YOUR-IID-TOKEN
.Running the app using the Firebase CLI:
npm install
to install dependencies.npm run build
to build the app using Vite.firebase emulators:start
to start the local Firebase emulators. Note: phone authentication required ReCaptcha verification which does not work with the Firebase emulators. These examples skip connecting to the emulators.127.0.0.1:5002
, though it may be different for you.firebase emulators:start
command.YOUR-SERVER-KEY
and YOUR-IID-TOKEN
.To deploy the sample app to production:
firebase deploy
.
This will deploy the sample app to https://<project_id>.firebaseapp.com
.NOTE: If your payload has a notification
object, setBackgroundMessageHandler
will not trigger. Read here for more information.
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Authorization: key=YOUR-SERVER-KEY
Content-Type: application/json
{
"notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1",
"icon": "firebase-logo.png",
"click_action": "http://localhost:8081"
},
"to": "YOUR-IID-TOKEN"
}
var key = 'YOUR-SERVER-KEY';
var to = 'YOUR-IID-TOKEN';
var notification = {
'title': 'Portugal vs. Denmark',
'body': '5 to 1',
'icon': 'firebase-logo.png',
'click_action': 'http://localhost:8081'
};
fetch('https://fcm.googleapis.com/fcm/send', {
'method': 'POST',
'headers': {
'Authorization': 'key=' + key,
'Content-Type': 'application/json'
},
'body': JSON.stringify({
'notification': notification,
'to': to
})
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
})
curl -X POST -H "Authorization: key=YOUR-SERVER-KEY" -H "Content-Type: application/json" -d '{
"notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1",
"icon": "firebase-logo.png",
"click_action": "http://localhost:8081"
},
"to": "YOUR-IID-TOKEN"
}' "https://fcm.googleapis.com/fcm/send"
When the app has the browser focus, the received message is handled through
the onMessage
callback in index.html
. When the app does not have browser
focus then the setBackgroundMessageHandler
callback in firebase-messaging-sw.js
is where the received message is handled.
The browser gives your app focus when both:
© Google, 2016. Licensed under an Apache-2 license.