forked from cbsandeep10/IMathAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCMsetup.php
More file actions
executable file
·123 lines (113 loc) · 3.92 KB
/
FCMsetup.php
File metadata and controls
executable file
·123 lines (113 loc) · 3.92 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
<?php
require("../init.php");
if (isset($_POST['token'])) {
$stm = $DBH->prepare("UPDATE imas_users SET FCMtoken=:token WHERE id=:id");
$stm->execute(array(":token"=>$_POST['token'], ":id"=>$userid));
echo "OK";
exit;
}
if (isset($_POST['remove'])) {
$stm = $DBH->prepare("UPDATE imas_users SET FCMtoken='' WHERE id=:id");
$stm->execute(array(":id"=>$userid));
echo "OK";
exit;
}
$placeinhead = '<script src="https://www.gstatic.com/firebasejs/3.5.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.5.3/firebase-messaging.js"></script>
<script>
// Initialize Firebase
var FCMconfig = {
apiKey: "'.Sanitize::encodeStringForJavascript($CFG['FCM']['webApiKey']).'",
messagingSenderId: "'.Sanitize::encodeStringForJavascript($CFG['FCM']['SenderId']).'"
};
firebase.initializeApp(FCMconfig);
const messaging = firebase.messaging();
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register(imasroot+"/firebase-messaging-sw.php").then(function(registration) {
// Registration was successful
console.log("ServiceWorker registration successful with scope: ", registration.scope);
messaging.useServiceWorker(registration);
if ("'.Sanitize::encodeStringForJavascript($FCMtoken).'" != "") {
messaging.getToken()
.then(function(token) {
if (token=="'.Sanitize::encodeStringForJavascript($FCMtoken).'") {
$("#havetoken").show(); $("#dosetup").hide();
}
})
.catch(function(err) {
console.log("Unable to retrieve token ", err);
});;
}
}).catch(function(err) {
// registration failed :(
console.log("ServiceWorker registration failed:" , err);
});
}
function removeNotifications() {
$.ajax({
url: "FCMsetup.php",
type: "POST",
data: {"remove": true}
}).done(function() {
$("#havetoken").hide(); $("#otherdevice").hide(); $("#dosetup").show(); $("#stopnotifications").hide();
});
}
function askPermission() {
messaging.requestPermission()
.then(function() {
console.log("Notification permission granted.");
return messaging.getToken();
})
.then(function(token) {
$("#havetoken").show(); $("#stopnotifications").show(); $("#dosetup").hide();
console.log("token:"+token);
$.ajax({
url: "FCMsetup.php",
type: "POST",
data: {"token": token}
});
})
.catch(function(err) {
console.log("Unable to get permission to notify.", err);
$("#havetoken").hide(); $("#dosetup").show();
});
}
messaging.onTokenRefresh(function() {
messaging.getToken()
.then(function(refreshedToken) {
console.log("Token refreshed.");
$.ajax({
url: "FCMsetup.php",
type: "POST",
data: {"token": refreshedToken}
});
})
.catch(function(err) {
console.log("Unable to retrieve refreshed token ", err);
});
});
messaging.onMessage(function(payload) {
console.log(payload);
});
</script>';
require("../header.php");
?>
<div class="breadcrumb"><a href="../index.php">Home</a> > <a href="../forms.php?action=chguserinfo">User Profile</a> > Notification Settings</div>
<div id="dosetup">
<p>If you would like to receive new message notifications from this system, even when not visiting this site,
click the button below. When your browser asks for permissions, click Allow. Note this system only works in some browsers
(Chrome and Firefox).</p>
<?php
if ($FCMtoken != '') {
echo '<p id="otherdevice">You already have notifications set up on another device. You can only receive notifications on one device at a time, so only click the button
below if you want to replace your current device with this one.</p>';
}
?>
<p><button type="button" onclick="askPermission()">Set up Notifications</button></p>
</div>
<div id="havetoken" style="display:none">Notifications are set up on this device.</div>
<p id="stopnotifications" <?php if ($FCMtoken=='') { echo 'style="display:none"';} ?>>
<button type="button" onclick="removeNotifications()">Stop Notifications</button></p>
<?php
require("../footer.php");
?>