如何在Json.stringy和Json.parse

时间:2018-01-18 16:11:34

标签: javascript html arrays json

我很难将我的移动应用程序弹出的通知发送到另一个页面并将其存储为历史记录..但在搜索并尝试了很多方法后,我似乎无法得到任何结果。 。

所以这里是 index.js 代码的一部分 - 其中一个函数是显示来自OneSignal的通知

document.addEventListener('deviceready', function () {
    // Enable to debug issues.
    // window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});

    var notificationOpenedCallback = function (jsonData) {
        console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
    };

    var notificationReceivedCallback = function (jsonData) {
        $("#message").html(jsonData.payload.body);  //display message received from onesignal
    };

    window.plugins.OneSignal
        .startInit("c651651-654e-987d-5f4a-65a4s65ddasd")
        .handleNotificationOpened(notificationOpenedCallback)
        .handleNotificationReceived(notificationReceivedCallback)
        .inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.None)
        .endInit();

}, false);

这就是我在index.js代码下面尝试过的 -

localStorage.setItem("notif", JSON.stringify(notificationReceivedCallback));

这是我创建的另一个页面,用于测试是否可以获取项目@ notif.html

<!DOCTYPE html>    
<html>
<head>
    <script src="www/scripts/index.js"></script>
    <script src="www/scripts/notif.js"></script>
    <title></title>
</head>
<body>
    <div>Content goes here.</div>
    <div id="printhere"></div>
</body>
</html>

notif.js

var notification = localStorage.getItem("notif");
$("#printhere").html("notification: ", JSON.parse(notification));

所以TLDR,我在index.html中弹出的oneSignal通知有点不好,因为它只能显示一个,如果有另一个通知弹出,它将替换当前的通知。 所以我希望在索引中弹出的通知以表格格式存储到notif.html / js中..作为记录或历史记录

所以请帮帮我,也许纠正我:)

非常感谢大家!

1 个答案:

答案 0 :(得分:0)

看起来您想要将通知附加到#printhere而不是替换已存在的内容,所以:

$("#printhere").html("notification: ", JSON.parse(notification));

变得像:

$("#printhere").append($("<p />").html("notification: " + JSON.parse(notification)));