firebase cloud messaging code example
Example 1: firebase cloud functions send email
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const cors = require('cors')({origin: true});
admin.initializeApp();
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourgmailaccpassword'
}
});
exports.sendMail = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const dest = req.query.dest;
const mailOptions = {
from: 'Your Account Name <[email protected]>',
to: dest,
subject: 'I\'M A PICKLE!!!',
html: `<p style="font-size: 16px;">Pickle Riiiiiiiiiiiiiiiick!!</p>
<br />
<img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
`
};
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
});
Example 2: getting firebase token
$ firebase token:ci
Example 3: firebase cloud function flutter
<head>
<link href="/your-path-to-fontawesome/css/all.css" rel="stylesheet"> <!--load all styles -->
</head>
<body>
<i class="fas fa-user"></i> <!-- uses solid style -->
<i class="far fa-user"></i> <!-- uses regular style -->
<i class="fal fa-user"></i> <!-- uses light style -->
<!--brand icon-->
<i class="fab fa-github-square"></i> <!-- uses brands style -->
</body>
Example 4: import firebase cloud messaging web
import ReactDOM from "react-dom";import App from "./App";if ("serviceWorker" in navigator) { navigator.serviceWorker .register("./firebase-messaging-sw.js") .then(function(registration) { console.log("Registration successful, scope is:", registration.scope); }) .catch(function(err) { console.log("Service worker registration failed, error:", err); });}ReactDOM.render(, document.getElementById("root"));