forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (137 loc) · 4.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support notifications");
}
const messaging = firebase.messaging();
const screens = ['#loading-screen', '#permission-screen', '#input-screen', '#waiting-screen'];
// Replace with HGE_URL
const HGE_URL = 'https://serverless-push.hasura.app/v1/graphql';
function showScreen(name) {
for (screen of screens) {
screen === name ? $(screen).show() : $(screen).hide();
}
}
function saveToken(token) {
window.localStorage.setItem('token', token);
}
function getTokenFromLocal() {
return window.localStorage.getItem('token');
}
function requestPermission() {
showScreen('#loading-screen');
messaging.requestPermission().then(function() {
console.log('Notification permission granted.');
$('#loading-text').html('📱 Registering device...');
getToken();
}).catch(function(err) {
console.log('Unable to get permission to notify.', err);
$('#loading-text').html('😥 Notification permission denied!');
});
}
function getToken() {
// Get Instance ID token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.getToken().then(function(currentToken) {
if (currentToken) {
console.log('got instance ID token');
saveToken(currentToken);
showScreen('#input-screen');
$('#text-input').focus();
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
showScreen('#permission-screen');
}
}).catch(function(err) {
console.error('An error occurred while retrieving token. ', err);
});
}
function notify(title) {
var notification = new Notification(title);
$('#waiting-ui').hide();
$('.notifications').show();
$('#notifications-list').append(`<li class="list-group-item list-group-item-light">${title}</li>`);
}
function tryAgain() {
resetSendButton();
showScreen('#input-screen');
$('#text-input').focus();
}
// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
// `messaging.setBackgroundMessageHandler` handler.
messaging.onMessage(function(payload) {
console.log('Message received. ', payload);
notify(payload.notification.title);
});
function resetSendButton() {
$('#title-submit').html('🔔 Send');
$('#text-input').val('');
}
function submitText() {
var textInput = $('#text-input').val();
if (!textInput) {
$('#title-submit').html('✋ Type something and click again');
return;
}
$('#title-submit').html('Sending...');
const r = new Request(HGE_URL);
const o = {
method: 'POST',
body: JSON.stringify({
query: `
mutation sendNotification($token: String!, $title: String!, $body: String) {
insert_message(
objects:{
device_token: $token,
title: $title,
body: $body
},
) { affected_rows }
}
`,
variables: {title: textInput, token: getTokenFromLocal()}
})
};
fetch(r, o).then(function(response) {
if (response.status === 200) {
console.log('request sent to server');
showScreen('#waiting-screen');
$('#waiting-ui').show();
} else {
console.error('An error happened while sending the request', response.statusText);
$('#error-text').html('An error happened, check console for details');
}
}).catch(function(err) {
console.error('An error occured while sending the request', err);
$('#error-text').html('An error happened, check console for details');
});
}
$( document ).ready(function() {
navigator.serviceWorker.register('firebase-messaging-sw.js')
.then((registration) => {
messaging.useServiceWorker(registration);
// Replace with FCM_PUBLIC_KEY
messaging.usePublicVapidKey('BOfaiApSMLjFZVYX_s4gRqb3LbrKcwtbv4qmIYtdZtE6UskL3rxCqBa5hCLhVXjFYsTmA6M8eW-aFZpTQa4B80E');
// Callback fired if Instance ID token is updated.
messaging.onTokenRefresh(function() {
messaging.getToken().then(function(refreshedToken) {
console.log('Token refreshed.');
saveToken(refreshedToken);
}).catch(function(err) {
console.error('Unable to retrieve refreshed token ', err);
});
});
getToken();
}).catch(function(err) {
console.error('An error occurred while registering service worker. ', err);
});
$('#text-input').on('keyup', function (e) {
if (e.keyCode == 13) {
submitText();
}
});
$('#hge-console-link').attr('href', HGE_URL.replace('v1/graphql', 'console'));
});