Facebook Messenger Webview可以访问LocalStorage API以在客户端存储数据吗?



如果他到达一个城市的 6 个站点,我想在用户端存储 6 个布尔值。 我可以在Messenger Webview中使用LocalStorage甚至cookie吗?

如果我关闭 Web 视图并再次打开它,数据是否仍然存在?

测试后,是的,它可以。 因此,您可以轻松地打开 Web 视图的按钮,就像在 botfile 应用程序中一样.js在服务器上

function openWebview(sender_psid, text,button_text){
let button_response = {
"attachment": {
"type": "template",
"payload": {
"template_type": "button",
"text": text,
"buttons": [
{
"type":"web_url",
"url":"<URL>",
"title":button_text,
"webview_height_ratio": "compact",
"messenger_extensions": "true",
"fallback_url": "<FB_URL>"
}
],
}
}
}
callSendAPI(sender_psid, button_response);
}

要完成...

function callSendAPI(sender_psid, response) {
// Construct the message body
let request_body = {
"recipient": {
"id": sender_psid
},
"message": response
}
request({
"uri": "https://graph.facebook.com/v2.6/me/messages",
"qs": { "access_token": PAGE_ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log('message sent!')
} else {
console.error("Unable to send message:" + err);
}
});
}

在索引上.html在客户端上,您可以存储简单的数据,就像

<div id='myDiv'>nothing for the moment</div>
<script>
localStorage.setItem('key1','myValue1')
var test = localStorage.getItem('key1')
document.getElementById('myDiv').innerHTML = test
</script>

您现在将在Facebook Messenger Webview上看到"myValue1"显示。

最新更新