The Firebase Cloud Messaging quickstart demonstrates how to:
- Request permission to send app notifications to the user.
- Receive FCM messages using the Firebase Cloud Messaging JavaScript SDK.
- Set up your project on the Firebase Console.
- Paste initialization snippet into
index.html
with the one generated from the Firebase Console Overview > Add Firebase to your web app. See TODO inindex.html
. - Run the app
- Install the Firebase CLI
- Use command
firebase serve -p 8081
to serve app locally. - Open http://localhost:8081 in your browser.
- Click REQUEST PERMISSION button to request permission for the app to send notifications to the browser.
- Use the generated Instance ID token to send an HTTP request to FCM that delivers the message to the web application, inserting appropriate values for YOUR-SERVER-KEY and YOUR-IID-TOKEN.
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"
}
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 thesetBackgroundMessageHandler
callback in firebase-messaging-sw.js is where the received message is handled.
The browser gives your app focus when both:
- Your app is running in the currently selected browser tab.
- The browser tab's window currently has focus, as defined by the operating system.
https://firebase.google.com/support/
© Google, 2016. Licensed under an Apache-2 license.