diff --git a/CHANGES.rst b/CHANGES.rst
index 21614d4..383fc6d 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,6 +1,10 @@
CHANGE LOG
==========
+1.0.1
+-----
+- Added in-app Notification
+
1.0.0
-----
- PWA Foundation
diff --git a/VERSION b/VERSION
index 3eefcb9..7dea76e 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.0.0
+1.0.1
diff --git a/molo/pwa/templates/fcm.js b/molo/pwa/templates/fcm.js
new file mode 100644
index 0000000..f761185
--- /dev/null
+++ b/molo/pwa/templates/fcm.js
@@ -0,0 +1,125 @@
+// Script loader
+function script(url) {
+
+ if(Array.isArray(url)) {
+ var self = this, prom = [];
+ url.forEach(function(item) {
+ prom.push(self.script(item));
+ });
+ return Promise.all(prom);
+ }
+
+ return new Promise(function (resolve, reject) {
+ var r = false,
+ t = document.getElementsByTagName("script")[0],
+ s = document.createElement("script");
+
+ s.type = "text/javascript";
+ s.src = url;
+ s.async = true;
+ s.onload = s.onreadystatechange = function () {
+ if (!r && (!this.readyState || this.readyState == "complete")) {
+ r = true;
+ resolve(this);
+ }
+ };
+ s.onerror = s.onabort = reject;
+ t.parentNode.insertBefore(s, t);
+ });
+
+}
+
+var loadFCM = script(["https://www.gstatic.com/firebasejs/4.1.3/firebase-app.js", "https://www.gstatic.com/firebasejs/4.1.3/firebase-messaging.js", "/toast.min.js"]);
+
+loadFCM.then(function() {
+
+ firebase.initializeApp(fcmConfig);
+
+ // Get X-CSRFToken
+ function parse_cookies() {
+ var cookies = {};
+ if (document.cookie && document.cookie !== '') {
+ document.cookie.split(';').forEach(function (c) {
+ var m = c.trim().match(/(\w+)=(.*)/);
+ if(m !== undefined) {
+ cookies[m[1]] = decodeURIComponent(m[2]);
+ }
+ });
+ }
+ return cookies;
+ }
+
+ var cookies = parse_cookies();
+
+ // Token magic
+ const messaging = firebase.messaging();
+ navigator.serviceWorker.register('/serviceworker.js')
+ .then((registration) => {
+ console.log(registration);
+ messaging.useServiceWorker(registration);
+
+ messaging.onTokenRefresh(function() {
+ messaging.getToken()
+ .then(function(refreshedToken) {
+ setTokenSentToServer(false);
+ sendTokenToServer(refreshedToken);
+ });
+ });
+
+ function sendTokenToServer(currentToken) {
+ if (!isTokenSentToServer()) {
+ fetch('/notification_devices/', {
+ method: "POST",
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-CSRFToken': cookies['csrftoken']
+ },
+ body: JSON.stringify({
+ 'registration_id': currentToken,
+ 'type': 'web',
+ }),
+ credentials: "include",
+ })
+ setTokenSentToServer(true);
+ }
+ }
+
+ function isTokenSentToServer() {
+ return window.localStorage.getItem('sentToServer') == 1;
+ }
+ function setTokenSentToServer(sent) {
+ window.localStorage.setItem('sentToServer', sent ? 1 : 0);
+ }
+
+ function requestPermission() {
+ messaging.requestPermission()
+ .then(function() {
+ messaging.getToken()
+ .then(function(currentToken) {
+ if (currentToken) {
+ sendTokenToServer(currentToken);
+ } else {
+ setTokenSentToServer(false);
+ }
+ })
+ .catch(function(err) {
+ setTokenSentToServer(false);
+ });
+ });
+ }
+
+ requestPermission();
+
+ // In app notification using Toast
+ messaging.onMessage(function(data) {
+ var options = {
+ settings: {
+ duration: 3000
+ }
+ };
+ iqwerty.toast.Toast(data.notification.title + ' - ' + data.notification.body, options);
+ });
+
+ });
+
+});
diff --git a/molo/pwa/templates/molo_pwa.html b/molo/pwa/templates/molo_pwa.html
index f83a217..068b9cc 100644
--- a/molo/pwa/templates/molo_pwa.html
+++ b/molo/pwa/templates/molo_pwa.html
@@ -7,105 +7,29 @@
+
+{% if request.user.is_authenticated %}
+
+{% endif %}
+
-{% if request.user.is_authenticated %}
-
-
-
-
-
-{% endif %}
diff --git a/molo/pwa/templates/toast.min.js b/molo/pwa/templates/toast.min.js
new file mode 100755
index 0000000..dd52219
--- /dev/null
+++ b/molo/pwa/templates/toast.min.js
@@ -0,0 +1 @@
+"use strict";var iqwerty=iqwerty||{};iqwerty.toast=function(){function t(o,r,i){if(null!==e())t.prototype.toastQueue.push({text:o,options:r,transitions:i});else{t.prototype.Transitions=i||n;var a=r||{};a=t.prototype.mergeOptions(t.prototype.DEFAULT_SETTINGS,a),t.prototype.show(o,a),a=null}}function e(){return i}function o(t){i=t}var r=400,n={SHOW:{"-webkit-transition":"opacity "+r+"ms, -webkit-transform "+r+"ms",transition:"opacity "+r+"ms, transform "+r+"ms",opacity:"1","-webkit-transform":"translateY(-100%) translateZ(0)",transform:"translateY(-100%) translateZ(0)"},HIDE:{opacity:"0","-webkit-transform":"translateY(150%) translateZ(0)",transform:"translateY(150%) translateZ(0)"}},i=null;return t.prototype.DEFAULT_SETTINGS={style:{main:{background:"rgba(0, 0, 0, .85)","box-shadow":"0 0 10px rgba(0, 0, 0, .8)","border-radius":"3px","z-index":"99999",color:"rgba(255, 255, 255, .9)",padding:"10px 15px","max-width":"60%",width:"100%","word-break":"keep-all",margin:"0 auto","text-align":"center",position:"fixed",left:"0",right:"0",bottom:"0","-webkit-transform":"translateY(150%) translateZ(0)",transform:"translateY(150%) translateZ(0)","-webkit-filter":"blur(0)",opacity:"0"}},settings:{duration:4e3}},t.prototype.Transitions={},t.prototype.toastQueue=[],t.prototype.timeout=null,t.prototype.mergeOptions=function(e,o){var r=o;for(var n in e)r.hasOwnProperty(n)?null!==e[n]&&e[n].constructor===Object&&(r[n]=t.prototype.mergeOptions(e[n],r[n])):r[n]=e[n];return r},t.prototype.generate=function(r,n){var i=document.createElement("div");"string"==typeof r&&(r=document.createTextNode(r)),i.appendChild(r),o(i),i=null,t.prototype.stylize(e(),n)},t.prototype.stylize=function(t,e){Object.keys(e).forEach(function(o){t.style[o]=e[o]})},t.prototype.show=function(o,r){this.generate(o,r.style.main);var n=e();document.body.insertBefore(n,document.body.firstChild),n.offsetHeight,t.prototype.stylize(n,t.prototype.Transitions.SHOW),n=null,clearTimeout(t.prototype.timeout),t.prototype.timeout=setTimeout(t.prototype.hide,r.settings.duration)},t.prototype.hide=function(){var o=e();t.prototype.stylize(o,t.prototype.Transitions.HIDE),clearTimeout(t.prototype.timeout),o.addEventListener("transitionend",t.prototype.animationListener),o=null},t.prototype.animationListener=function(){e().removeEventListener("transitionend",t.prototype.animationListener),t.prototype.destroy.call(this)},t.prototype.destroy=function(){var r=e();if(document.body.removeChild(r),r=null,o(null),t.prototype.toastQueue.length>0){var n=t.prototype.toastQueue.shift();t(n.text,n.options,n.transitions),n=null}},{Toast:t}}(),"undefined"!=typeof module&&(module.exports=iqwerty.toast);
\ No newline at end of file
diff --git a/molo/pwa/urls.py b/molo/pwa/urls.py
index 35cd658..350f0e0 100644
--- a/molo/pwa/urls.py
+++ b/molo/pwa/urls.py
@@ -3,7 +3,7 @@
from fcm_django.api.rest_framework import FCMDeviceViewSet
from rest_framework.routers import DefaultRouter
-from .views import RegistrationTokenView, manifest, service_worker
+from .views import RegistrationTokenView, manifest, service_worker, fcm, toast
urlpatterns = patterns('', )
@@ -18,4 +18,6 @@
name='registration-token'),
url('^serviceworker.js$', service_worker),
url('^manifest.json$', manifest),
+ url('^fcm.js$', fcm),
+ url('^toast.min.js$', toast),
)
diff --git a/molo/pwa/views.py b/molo/pwa/views.py
index 91cb70c..f314c84 100644
--- a/molo/pwa/views.py
+++ b/molo/pwa/views.py
@@ -34,3 +34,13 @@ def manifest(request):
for setting_name in dir(app_settings)
if setting_name.startswith('PWA_')
})
+
+
+def fcm(request):
+ return render(request, 'fcm.js',
+ content_type='application/javascript')
+
+
+def toast(request):
+ return render(request, 'toast.min.js',
+ content_type='application/javascript')