-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
111 lines (96 loc) · 3.36 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.0/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.2/firebase-database.js"></script>
<script type="text/javascript">
// Please use your own firebaseConfig provided by firebase for your site.
var firebaseConfig = {
apiKey: "<FIREBASE-APIKEY",
authDomain: "<AUTH-DOMAIN>",
databaseURL: "<DB-URL>",
projectId: "<PROJECT-ID>",
storageBucket: "<BUCKET>",
messagingSenderId: "<SENDER-ID",
appId: "APP-ID"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('Notification permission granted.');
if(isTokenSentToServer()){
console.log('Token already sent ');
}else{
getRegisteredToken();
}
} else {
console.log('Unable to get permission to notify.');
}
});
function getRegisteredToken() {
messaging.getToken().then((currentToken) => {
if (currentToken) {
saveToken(currentToken);
sendTokenToServer(currentToken);
//updateUIForPushEnabled(currentToken);
} else {
console.log('No Instance ID token available. Request permission to generate one.');
//updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
setTokenSentToServer(false);
});
}
function sendTokenToServer(currentToken) {
if (!isTokenSentToServer()) {
console.log('Sending token to server...');
setTokenSentToServer(true);
} else {
console.log('Token already sent to server so won\'t send it again ' +
'unless it changes');
}
}
function setTokenSentToServer(sent) {
window.localStorage.setItem('sentToServer', sent ? '1' : '0');
}
function isTokenSentToServer() {
return window.localStorage.getItem('sentToServer') === '1';
}
function getToken(currentToken) {
return currentToken;
}
function saveToken(currentToken) {
$.ajax({
type: "POST",
url: "https://api.cronberry.com/cronberry/api/campaign/register-audience-data",
dataType: "json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
data:JSON.stringify({
projectKey: "<CRONBERRY-APIKEY>",
audienceId: "<Audience ID>", //If you do not have audience id, use audienceId : window.navigator.userAgent.replace(/\D+/g, '')
web_fcm_token: currentToken
}),
success: function (data) {
console.log("success");
},
error: function () {
console.log("error");
}
});
}
</script>
</head>
<body>
<h1>Cronberry JavaScript for webpush integration</h1>
</body>
</html>