From 2472dfdd7ebd7b4e862459440866df70d9879640 Mon Sep 17 00:00:00 2001 From: Daniel <nieltansah@gmail.com> Date: Thu, 23 Nov 2017 20:54:20 +0700 Subject: [PATCH] WebApp: Add Firebase connectors Note: This commit contains Firebase token. --- .../src/main/webapp/firebase-messaging-sw.js | 15 +++ .../WebApp/src/main/webapp/js/converse.js | 92 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 TugasBesar2_2017/WebApp/src/main/webapp/firebase-messaging-sw.js create mode 100644 TugasBesar2_2017/WebApp/src/main/webapp/js/converse.js diff --git a/TugasBesar2_2017/WebApp/src/main/webapp/firebase-messaging-sw.js b/TugasBesar2_2017/WebApp/src/main/webapp/firebase-messaging-sw.js new file mode 100644 index 0000000..7f9bf37 --- /dev/null +++ b/TugasBesar2_2017/WebApp/src/main/webapp/firebase-messaging-sw.js @@ -0,0 +1,15 @@ +// 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/4.6.2/firebase-app.js') +importScripts('https://www.gstatic.com/firebasejs/4.6.2/firebase-messaging.js') + +// Initialize the Firebase app in the service worker by passing in the +// messagingSenderId. +firebase.initializeApp({ + 'messagingSenderId': '948017499394' +}) + +// Retrieve an instance of Firebase Messaging so that it can handle background +// messages. +const messaging = firebase.messaging() diff --git a/TugasBesar2_2017/WebApp/src/main/webapp/js/converse.js b/TugasBesar2_2017/WebApp/src/main/webapp/js/converse.js new file mode 100644 index 0000000..e7cb432 --- /dev/null +++ b/TugasBesar2_2017/WebApp/src/main/webapp/js/converse.js @@ -0,0 +1,92 @@ + +let converse = function ($http) { + let apiUrl = 'http://localhost:8081' + + // Initialize Firebase + let config = { + apiKey: "AIzaSyCUq3YUcVzUkUpIyXmpGd_L43JX_xKyq64", + authDomain: "tugasbesar3-2017.firebaseapp.com", + databaseURL: "https://tugasbesar3-2017.firebaseio.com", + projectId: "tugasbesar3-2017", + storageBucket: "tugasbesar3-2017.appspot.com", + messagingSenderId: "948017499394" + } + + class Account { + constructor (id) { + this.id = id + this.onchat = (msg) => { + console.log(msg) + } + } + + async initAsync () { + firebase.initializeApp(config) + let messaging = firebase.messaging() + + messaging.onMessage(function (msg) { + console.log(msg) + console.log("Hi") + this.onchat(msg.data.message) + }) + + try { + await messaging.requestPermission() + } catch (e) { + console.log(e) + alert('Please grant notification permission.') + } + + try { + let data = JSON.stringify({ + 'token': await messaging.getToken() + }) + + console.log(data) + await $http.post(`${apiUrl}/chat/${this.id}/token`, data) + } catch (e) { + console.log(e) + alert('Unable to initialize chat service.') + } + } + + chatAsync (message) { + let data = JSON.stringify({ + 'message': message + }) + + return $http.post(`${apiUrl}/chat/${this.id}`, data) + } + } + + class Driver extends Account { + async waitAsync (locations, timeout) { + let response = await $http.post(`${apiUrl}/driver/${this.id}`, null, { + timeout: timeout + }) + + return response.data.driver_id + } + } + + class User extends Account { + async listAsync (location) { + let response = await $http.get(`${apiUrl}/driver/find?location=${location}`) + + return response.data.driver_ids + } + + pickAsync (id) { + let data = JSON.stringify({ + 'user_id': id + }) + + return $http.post(`${apiUrl}/driver/${this.id}/pick`, data) + } + } + + return { + Driver: Driver, + User: User + } +} -- GitLab