Skip to content

Commit 1f7e6e9

Browse files
authored
Move quickstart-js snippets (#86)
1 parent 6344eeb commit 1f7e6e9

File tree

6 files changed

+181
-1
lines changed

6 files changed

+181
-1
lines changed

lerna.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
"functions",
1111
"functions-next",
1212
"installations",
13+
"messaging",
1314
"perf",
1415
"perf-next",
1516
"remoteconfig",
16-
"remoteconfig-next"
17+
"remoteconfig-next",
18+
"storage"
1719
],
1820
"version": "1.0.0"
1921
}

messaging/index.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import firebase from "firebase/app";
2+
import "firebase/messaging";
3+
4+
function getMessagingObject() {
5+
// [START messaging_get_messaging_object]
6+
const messaging = firebase.messaging();
7+
// [END messaging_get_messaging_object]
8+
}
9+
10+
function receiveMessage() {
11+
const messaging = firebase.messaging();
12+
// [START messaging_receive_message]
13+
// Handle incoming messages. Called when:
14+
// - a message is received while the app has focus
15+
// - the user clicks on an app notification created by a service worker
16+
// `messaging.onBackgroundMessage` handler.
17+
messaging.onMessage((payload) => {
18+
console.log('Message received. ', payload);
19+
// ...
20+
});
21+
// [END messaging_receive_message]
22+
}
23+
24+
function getToken() {
25+
const messaging = firebase.messaging();
26+
// [START messaging_get_token]
27+
// Get registration token. Initially this makes a network call, once retrieved
28+
// subsequent calls to getToken will return from cache.
29+
messaging.getToken({ vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then((currentToken) => {
30+
if (currentToken) {
31+
// Send the token to your server and update the UI if necessary
32+
// ...
33+
} else {
34+
// Show permission request UI
35+
console.log('No registration token available. Request permission to generate one.');
36+
// ...
37+
}
38+
}).catch((err) => {
39+
console.log('An error occurred while retrieving token. ', err);
40+
// ...
41+
});
42+
// [END messaging_get_token]
43+
}
44+
45+
function requestPermission() {
46+
// [START messaging_request_permission]
47+
Notification.requestPermission().then((permission) => {
48+
if (permission === 'granted') {
49+
console.log('Notification permission granted.');
50+
// TODO(developer): Retrieve a registration token for use with FCM.
51+
// ...
52+
} else {
53+
console.log('Unable to get permission to notify.');
54+
}
55+
});
56+
// [END messaging_request_permission]
57+
}
58+
59+
function deleteToken() {
60+
const messaging = firebase.messaging();
61+
62+
// [START messaging_delete_token]
63+
messaging.deleteToken().then(() => {
64+
console.log('Token deleted.');
65+
// ...
66+
}).catch((err) => {
67+
console.log('Unable to delete token. ', err);
68+
});
69+
// [END messaging_delete_token]
70+
}

messaging/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "messaging",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc"
6+
},
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"firebase": "^8.2.3"
10+
}
11+
}

messaging/service-worker.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import firebase from "firebase/app";
2+
import "firebase/messaging";
3+
4+
// See: https://github.com/microsoft/TypeScript/issues/14877
5+
/** @type {ServiceWorkerGlobalScope} */
6+
let self;
7+
8+
function initInSw() {
9+
// [START messaging_init_in_sw]
10+
// Give the service worker access to Firebase Messaging.
11+
// Note that you can only use Firebase Messaging here. Other Firebase libraries
12+
// are not available in the service worker.
13+
importScripts('https://www.gstatic.com/firebasejs/8.2.3/firebase-app.js');
14+
importScripts('https://www.gstatic.com/firebasejs/8.2.3/firebase-messaging.js');
15+
16+
// Initialize the Firebase app in the service worker by passing in
17+
// your app's Firebase config object.
18+
// https://firebase.google.com/docs/web/setup#config-object
19+
firebase.initializeApp({
20+
apiKey: 'api-key',
21+
authDomain: 'project-id.firebaseapp.com',
22+
databaseURL: 'https://project-id.firebaseio.com',
23+
projectId: 'project-id',
24+
storageBucket: 'project-id.appspot.com',
25+
messagingSenderId: 'sender-id',
26+
appId: 'app-id',
27+
measurementId: 'G-measurement-id',
28+
});
29+
30+
// Retrieve an instance of Firebase Messaging so that it can handle background
31+
// messages.
32+
const messaging = firebase.messaging();
33+
// [END messaging_init_in_sw]
34+
}
35+
36+
function onBackgroundMessage() {
37+
const messaging = firebase.messaging();
38+
39+
// [START messaging_on_background_message]
40+
messaging.onBackgroundMessage((payload) => {
41+
console.log('[firebase-messaging-sw.js] Received background message ', payload);
42+
// Customize notification here
43+
const notificationTitle = 'Background Message Title';
44+
const notificationOptions = {
45+
body: 'Background Message body.',
46+
icon: '/firebase-logo.png'
47+
};
48+
49+
self.registration.showNotification(notificationTitle,
50+
notificationOptions);
51+
});
52+
// [END messaging_on_background_message]
53+
}

storage/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import firebase from "firebase/app";
2+
import "firebase/storage";
3+
4+
/**
5+
* @param {File} file
6+
*/
7+
function storageOnComplete(file) {
8+
// The file param would be a File object from a file selection event in the browser.
9+
// See:
10+
// - https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications
11+
// - https://developer.mozilla.org/en-US/docs/Web/API/File
12+
13+
const metadata = {
14+
'contentType': file.type
15+
};
16+
17+
// [START storage_on_complete]
18+
const storageRef = firebase.storage().ref();
19+
storageRef.child('images/' + file.name).put(file, metadata)
20+
.then((snapshot) => {
21+
console.log('Uploaded', snapshot.totalBytes, 'bytes.');
22+
console.log('File metadata:', snapshot.metadata);
23+
// Let's get a download URL for the file.
24+
snapshot.ref.getDownloadURL().then((url) => {
25+
console.log('File available at', url);
26+
// ...
27+
});
28+
}).catch((error) => {
29+
console.error('Upload failed', error);
30+
// ...
31+
});
32+
// [END storage_on_complete]
33+
}

storage/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "storage",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc"
6+
},
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"firebase": "^8.2.3"
10+
}
11+
}

0 commit comments

Comments
 (0)