8000 Use Implicit Firebase initialization in the FCM sample. · iambryansanders/quickstart-js@524d22f · GitHub
[go: up one dir, main page]

Skip to content

Commit 524d22f

Browse files
author
Nicolas Garnier
committed
Use Implicit Firebase initialization in the FCM sample.
Change-Id: Ibdcd25566d1fb5e5744a4c1cdc3658ff0f61ae92
1 parent e6b4da8 commit 524d22f

File tree

4 files changed

+56
-43
lines changed

4 files changed

+56
-43
lines changed

messaging/README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,19 @@ The Firebase Cloud Messaging quickstart demonstrates how to:
88
Introduction
99
------------
1010

11-
- [Read more about Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
11+
[Read more about Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
1212

1313
Getting Started
1414
---------------
1515

16-
1. Set up your project on the [Firebase Console](https://console.firebase.google.com).
17-
2. Paste initialization snippet into `index.html` with the one generated from
18-
the Firebase Console **Overview > Add Firebase to your web app**. See TODO in
19-
`index.html`.
20-
3. Run the app
21-
- Install 8000 the [Firebase CLI](https://firebase.google.com/docs/cli/)
22-
- Use command `firebase serve -p 8081` to serve app locally.
23-
- Open http://localhost:8081 in your browser.
24-
4. Click REQUEST PERMISSION button to request permission for the app to send
25-
notifications to the browser.
26-
5. Use the generated Instance ID token to send an HTTP request to FCM that
27-
delivers the message to the web application, inserting appropriate values
28-
for [YOUR-SERVER-KEY](https://console.firebase.google.com/project/_/settings/cloudmessaging)
29-
and YOUR-IID-TOKEN.
16+
1. Create your project on the [Firebase Console](https://console.firebase.google.com).
17+
1. Enable the **Google** sign-in provider in the **Authentication > SIGN-IN METHOD** tab.
18+
1. You must have the Firebase CLI installed. If you don't have it install it with `npm install -g firebase-tools` and then configure it with `firebase login`.
19+
1. On the command line run `firebase use --add` and select the Firebase project you have created.
20+
1. On the command line run `firebase serve -p 8081` using the Firebase CLI tool to launch a local server.
21+
1. Open [http://localhost:8081](http://localhost:8081) in your browser.
22+
4. Click **REQUEST PERMISSION** button to request permission for the app to send notifications to the browser.
23+
5. Use the generated Instance ID token (IID Token) to send an HTTP request to FCM that delivers the message to the web application, inserting appropriate values for [`YOUR-SERVER-KEY`](https://console.firebase.google.com/project/_/settings/cloudmessaging) and `YOUR-IID-TOKEN`.
3024

3125
### HTTP
3226
```

messaging/firebase-messaging-sw.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
// [START initialize_firebase_in_sw]
2-
// Give the service worker access to Firebase Messaging.
3-
// Note that you can only use Firebase Messaging here, other Firebase libraries
4-
// are not available in the service worker.
5-
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
6-
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
7-
8-
// Initialize the Firebase app in the service worker by passing in the
9-
// messagingSenderId.
10-
firebase.initializeApp({
11-
'messagingSenderId': 'YOUR-SENDER-ID'
12-
});
1+
// Import and configure the Firebase SDK
2+
// These scripts are made available when the app is served or deployed on Firebase Hosting
3+
// If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup
4+
importScripts('/__/firebase/3.9.0/firebase-app.js');
5+
importScripts('/__/firebase/3.9.0/firebase-messaging.js');
6+
importScripts('/__/firebase/init.js');
137

14-
// Retrieve an instance of Firebase Messaging so that it can handle background
15-
// messages.
168
const messaging = firebase.messaging();
17-
// [END initialize_firebase_in_sw]
9+
10+
/**
11+
* Here is is the code snippet to initialize Firebase Messaging in the Service
12+
* Worker when your app is not hosted on Firebase Hosting.
13+
14+
// [START initialize_firebase_in_sw]
15+
// Give the service worker access to Firebase Messaging.
16+
// Note that you can only use Firebase Messaging here, other Firebase libraries
17+
// are not available in the service worker.
18+
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
19+
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
20+
21+
// Initialize the Firebase app in the service worker by passing in the
22+
// messagingSenderId.
23+
firebase.initializeApp({
24+
'messagingSenderId': 'YOUR-SENDER-ID'
25+
});
26+
27+
// Retrieve an instance of Firebase Messaging so that it can handle background
28+
// messages.
29+
const messaging = firebase.messaging();
30+
// [END initialize_firebase_in_sw]
31+
**/
32+
1833

1934
// If you would like to customize notifications that are received in the
2035
// background (Web app is closed or not in browser focus) then you should

messaging/firebase.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"hosting": {
3+
"public": "./",
4+
"ignore": [
5+
"firebase.json"
6+
]
7+
}
8+
}

messaging/index.html

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,14 @@ <h4>Needs Permission</h4>
7171
</div>
7272
</main>
7373
</div>
74-
<!-- Firebase -->
75-
<!-- ********************************************************
76-
* TODO(DEVELOPER): Update Firebase initialization code:
77-
1. Go to the Firebase console: https://console.firebase.google.com/
78-
2. Choose a Firebase project you've created
79-
3. Click "Add Firebase to your web app"
80-
4. Replace the following initialization code with the code from the Firebase console:
81-
-->
82-
<!-- START INITIALIZATION CODE -->
83-
<!-- PASTE FIREBASE INITIALIZATION CODE HERE -->
84-
<!-- END INITIALIZATION CODE -->
85-
<!-- ******************************************************** -->
74+
75+
<!-- Import and configure the Firebase SDK -->
76+
<!-- These scripts are made available when the app is served or deployed on Firebase Hosting -->
77+
<!-- If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup -->
78+
<script src="/__/firebase/3.9.0/firebase-app.js"></script>
79+
<script src="/__/firebase/3.9.0/firebase-messaging.js"></script>
80+
<script src="/__/firebase/init.js"></script>
81+
8682
<script>
8783
// [START get_messaging_object]
8884
// Retrieve Firebase Messaging object.
@@ -180,7 +176,7 @@ <h4>Needs Permission</h4>
180176

181177
function isTokenSentToServer() {
182178
if (window.localStorage.getItem('sentToServer') == 1) {
183-
return true;
179+
return true;
184180
}
185181
return false;
186182
}

0 commit comments

Comments
 (0)
0