如标题中所述,单击通知后,我无法重定向到URL。这是我的代码。 // Index.php
<html>
<meta charset="utf-8"/>
<title>Firebase Messaging </title>
<link rel="manifest" href="manifest.json">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
div {
margin-bottom: 15px;
}
</style>
<body>
<div id="token"></div>
<div id="msg"></div>
<div id="notis"></div>
<div id="err"></div>
<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script>
MsgElem = document.getElementById("msg")
TokenElem = document.getElementById("token")
NotisElem = document.getElementById("notis")
ErrElem = document.getElementById("err")
var config = {
//some PRIVATE value
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging
.requestPermission()
.then(function () {
console.log("Notification permission granted.");
return messaging.getToken()
})
.then(function(token) {
sendToken(token);
setTokenSentToServer(true);
})
.catch(function (err) {
ErrElem.innerHTML = ErrElem.innerHTML + "; " + err
console.log("Unable to get permission to notify.", err);
});
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
notificationTitle = payload.data.title;
notificationOptions = {
body: payload.data.body,
icon: payload.data.icon,
click_action: payload.data.url,
};
var notification= new
Notification(notificationTitle,notificationOptions);
});
function setTokenSentToServer(sent)
{
window.localStorage.setItem('sentToServer', sent ? 1 : 0);
}
//this is ajax to send token to send.php
function sendToken(token)
{
$.ajax({
url: 'action.php',
method: 'POST',
data: 'tokens=' + token,
}).done(function(result){
console.log(result);
})
}
</script>
</body>
</html>
这是下一页,我在其中使用cookie来存储令牌并在下一页上使用它。
<?php
if(isset($_POST['tokens']))
{ if(isset($_COOKIE["tokens"]))
{$tokens=$_POST['tokens'];
}
else
{
$tokens=$_POST['tokens'];
setcookie("tokens", $tokens, time() + (86400 * 30), "/");
}
}
header("Location: send.php");
?>
然后最有可能在send.php上处理令牌
<?php
$tokens= array($_COOKIE["tokens"]);
$msg=[
'title'=>'Title',
'body'=>'description.',
'icon'=>'logo.jpg',
'url'=>"https://google.com"
];
$payload=[
'registration_ids'=>$tokens,
'data'=>$msg
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>json_encode($payload),
CURLOPT_HTTPHEADER => array(
"authorization: key=/*the key*/",
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
else
echo $response;
?>
最后是服务人员
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-
messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': 'MY_SENDER_ID'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ',payload);
// Customize notification here
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
icon: payload.data.icon,
click_action: payload.data.url,
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
通知进行得很好,但是每当我单击它时,它都不会重定向。谁能帮我解决这个代码。 预先感谢。