This is a collection of frequently used functions for Firebase Cloud Functions and/or Google Cloud Functions. The following steps are intended for use with a Firebase-project and the polymerfire elements. Please make sure you have a Polymer web app (preferably generated w/ the polymer-cli) up and running before continuing.
- Add
gcm_sender_id: "103953800509"
tomanifest.json
- Create
firebase-messaging-sw.js
and add it as dependency inpolymer.json
(get SENDER_ID fromSettings > Project Settings > Cloud Messaging
in the Firebase console):
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the messagingSenderId
firebase.initializeApp({
'messagingSenderId': '[ SENDER_ID ]'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
- Set the
messaging-sender-id
property onfirebase-app
element (get ID from above) - Create
functions
folder:
- Create
package.json
and add dependencies:
{
"name": "cloud-functions",
"description": "Firebase SDK for Cloud Functions",
"private": true,
"dependencies": {
"firebase-admin": "^5.5.0",
"firebase-functions": "^0.7.3",
"request": "^2.79.0",
"request-promise": "^4.2.1",
"secure-compare": "^3.0.1"
}
}
- Create
index.js
, add imports and init app:
// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
// Import the Firebase SDK for Google Cloud Functions
const functions = require('firebase-functions');
// Init
admin.initializeApp(functions.config().firebase);
// Setting api key
const MESSAGING_SERVER_KEY = functions.config().messaging.key;
- Set
SERVER_KEY
in environment config (get SERVER_KEY fromSettings > Project Settings > Cloud Messaging
):
firebase functions:config:set messaging.key="SERVER_KEY"
- Generate and set
CRON_KEY
in environment config:
npm install -g crypto
node -e "console.log(require('crypto').randomBytes(20).toString('hex'))"
firebase functions:config:set cron.key="CRON_KEY"
- Add export function snippets to the bottom of
index.js
- Lastly add
logStatus.snippet.js
to enable output to logs - Update
Security Rules
:
{
"rules": {
".read": false,
".write": false,
"topics": {
"$topic": {
"$uid": {
".read": "auth.uid === $uid",
".write": "auth.uid === $uid"
}
}
}
}
}
Install npm (or install Node):
curl -L https://www.npmjs.com/install.sh | sh
Install bower:
npm install -g bower
Install firebase-tools:
npm install -g firebase-tools
After installing prerequisites and tools:
firebase login
firebase use default
This command deploys the application to hosting in the current project. Use
firebase list
to print a list of all Firebase projects.
firebase deploy --only functions