-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
141 lines (120 loc) · 4.29 KB
/
test.html
File metadata and controls
141 lines (120 loc) · 4.29 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="manifest" href="manifest.json">
</head>
<body>
<h1>Hello, world!</h1>
<script src="js/jquery-3.4.1.min.js"></script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-messaging.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "ENTER_YOUR_FIREBASE_API_KEY",
authDomain: "web-push-notification-4b7d7.firebaseapp.com",
databaseURL: "https://web-push-notification-4b7d7.firebaseio.com",
projectId: "web-push-notification-4b7d7",
storageBucket: "web-push-notification-4b7d7.appspot.com",
messagingSenderId: "80760388858",
appId: "1:80760388858:web:719d5faa3617dc99015d43",
measurementId: "G-JJVT2WTC7R"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function () {
// console.log('have permission');
if (isTokenSentToServer()) {
// console.log('token already sent to server');
} else {
getRegistrationToken();
}
})
.catch(function (err) {
console.log('error occured');
});
function getRegistrationToken() {
// Get Instance ID token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.getToken().then((currentToken) => {
if (currentToken) {
saveToken(currentToken);
// console.log(currentToken);
sendTokenToServer(currentToken);
//updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
//updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
//showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
}
function setTokenSentToServer(sent) {
window.localStorage.setItem('sentToServer', sent ? '1' : '0');
}
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 saveToken(currentToken) {
$.ajax({
type: 'POST',
url: 'php/save-token.php',
dataType: "json",
data: {
token: currentToken
},
success: function (data) {
if (data.status = 'ok') {
console.log(data.status);
} else if (data.status = 601) {
console.log(data.error);
}
// console.log(data);
}
});
}
// 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((payload) => {
console.log('Message received. ', payload);
// ...
var title = payload.data.title;
var options = {
body: payload.data.body,
icon: payload.data.icon,
image: payload.data.image,
data: {
time: new Date(Date.now()).toString(),
click_action: payload.data.click_action
}
};
var myNotification = new Notification(title, options);
});
</script>
</body>
</html>