Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information
rakib-bs23 committed Feb 21, 2018
0 parents commit 47ff8d364a70ea1c2f88ef96118f7c8a62e09f84
Showing with 141 additions and 0 deletions.
  1. BIN alarm.png
  2. +9 −0 curl_command.txt
  3. BIN favicon.ico
  4. +22 −0 firebase-messaging-sw.js
  5. +27 −0 index.html
  6. +9 −0 manifest.json
  7. +74 −0 script.js
BIN +3.04 KB alarm.png
Binary file not shown.
@@ -0,0 +1,9 @@
curl -X POST -H "Authorization: key=AAAAFPQxzl0:APA91bHjc5Db0x1kHYcdMHBUzaRBDdYI0YiwVxshDpuPK8c9LQ-cboHKF87wcGI0BS_xZujkH0__cE61MSTV6ImiNVMkNFI7dEH-q6oJ4MTdkg50DSgsv4GwsabuYF7CWHD1X6lPNqZg" -H "Content-Type: application/json" -d '{
"notification": {
"title": "New Article",
"body": "Firebase Realtime Database using Python SDK",
"icon": "alarm.png",
"click_action": "http://rakibul.net/fb-realtime-db-python"
},
"to": "d9wiwOhj7ME:APA91bE2HagjEawPj0FTgo8Zv1Ur4zWsExgD0figYD3BC6NeLlMXKhYWFAPLqS1T6OUGVqKYb9KEuCJ9Ex6qeHo5mibaD76EVQvOrzzNSyRK-TQ4Dxexr48ujPFYdCpb-w71on3uNvAO"
}' "https://fcm.googleapis.com/fcm/send"
BIN +1.12 KB favicon.ico
Binary file not shown.
@@ -0,0 +1,22 @@
// Give the service worker access to Firebase Messaging.
importScripts('https://www.gstatic.com/firebasejs/4.9.1/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/4.9.1/firebase-messaging.js')

// Initialize the Firebase app in the service worker by passing in the messagingSenderId.
var config = {
messagingSenderId: "89996250717"
};
firebase.initializeApp(config);

// Retrieve an instance of Firebase Messaging so that it can handle background messages.
const messaging = firebase.messaging()
messaging.setBackgroundMessageHandler(function(payload) {
const notificationTitle = 'Message Title';
const notificationOptions = {
body: 'Message body',
icon: 'alarm.png'
};

return self.registration.showNotification(notificationTitle,
notificationOptions);
});
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="manifest" href="manifest.json">
<link rel="shortcut icon" href="favicon.ico">
<title>FCM Demo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
</head>
<body>
<h1 style="text-align:center;">
Firebase Cloud Messaging Demo
</h1>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.1/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.1/firebase-messaging.js"></script>
<script src="script.js"></script>
</body>
</html>

<!-- https://firebase.google.com/docs/cloud-messaging/js/receive -->
<!-- https://codelabs.developers.google.com/codelabs/firebase-web/#9 -->
<!-- https://github.com/firebase/friendlychat-web/tree/master/web -->
@@ -0,0 +1,9 @@
{
"short_name": "rakibul.net",
"name": "Rakibul Islam",
"background_color": "#ffffff",
"display": "standalone",
"theme_color": "#ffffff",
"start_url": "/",
"gcm_sender_id": "103953800507"
}
@@ -0,0 +1,74 @@
// Initialize the Firebase app by passing in the messagingSenderId
var config = {
messagingSenderId: "89996250717"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();

// Request for permission
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
// TODO(developer): Retrieve an Instance ID token for use with FCM.
messaging.getToken()
.then(function(currentToken) {
if (currentToken) {
console.log('Token: ' + currentToken)
sendTokenToServer(currentToken);
} else {
console.log('No Instance ID token available. Request permission to generate one.');
setTokenSentToServer(false);
}
})
.catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
setTokenSentToServer(false);
});
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});

// Handle incoming messages
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
toastr["info"](payload.notification.body, payload.notification.title);
});

// Callback fired if Instance ID token is updated.
messaging.onTokenRefresh(function() {
messaging.getToken()
.then(function(refreshedToken) {
console.log('Token refreshed.');
// Indicate that the new Instance ID token has not yet been sent
// to the app server.
setTokenSentToServer(false);
// Send Instance ID token to app server.
sendTokenToServer(refreshedToken);
})
.catch(function(err) {
console.log('Unable to retrieve refreshed token ', err);
});
});

// Send the Instance ID token your application server, so that it can:
// - send messages back to this app
// - subscribe/unsubscribe the token from topics
function sendTokenToServer(currentToken) {
if (!isTokenSentToServer()) {
console.log('Sending token to server...');
// TODO(developer): Send the current token to your server.
setTokenSentToServer(true);
} else {
console.log('Token already sent to server so won\'t send it again ' +
'unless it changes');
}
}

function isTokenSentToServer() {
return window.localStorage.getItem('sentToServer') == 1;
}

function setTokenSentToServer(sent) {
window.localStorage.setItem('sentToServer', sent ? 1 : 0);
}

0 comments on commit 47ff8d3

Please sign in to comment.
You can’t perform that action at this time.