使用localStorage和onResume时的Cordova问题

时间:2019-10-12 17:36:32

标签: cordova cordova-plugins

我使用的是Cordova平台版本:ios 4.5.5。这是我的index.js文件:

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
        document.addEventListener('resume', this.onResume.bind(this), false);
    },

    onResume: function() {
        alert('ON RESUME');
    };

    onDeviceReady: function() {
      StatusBar.backgroundColorByHexString('#FFFFFF');
      var push = PushNotification.init({
        android: {},
        browser: {},
        ios: {
          alert: "true",
          badge: "true",
          sound: "true"
        },
        windows: {}
      });

      push.on('notification', function (data) {
        console.log(data)
      });

      push.on('error', function (err) {
        console.log(err)
        alert('Event=error, message=' + err.message)
      });

      push.on('registration', function (data) {
        console.dir(data)
        console.log('registrationId:' + data.registrationId)

       window.localStorage.setItem("regId", data.registrationId);
      });

     alert('TEST' + window.localStorage.getItem("regId"))
     cordova.InAppBrowser.open('site_url', '_self');
    }
};

app.initialize();

1)设置localStorage时,第一次使用xCode在iPhone上进行远程调试启动应用程序时,它似乎无法正常工作。如果我停止模拟器并再次运行它,它似乎可以正常工作。

2)为了使我的应用正常工作,我需要注释掉onResume函数。我将其放置在错误的位置吗?我正在尝试在Mac上使用Safari Developer工具进行调试。但是没有看到任何有用的错误消息。该应用程序页面只会打开为空白页面,并且不会显示任何警报。

编辑:通过将事件监听器移到onDeviceReady函数内部,我能够加载应用程序:

onDeviceReady: function() {
      document.addEventListener("resume", function(){
        alert('ON RESUME');
      }, false)
...

但是,当我打开另一个应用程序然后重新打开我的应用程序时,我看不到警报出现。

0 个答案:

没有答案