【问题标题】:WP8.1 WinJS application crashes when launched from toast notification从 toast 通知启动时,WP8.1 WinJS 应用程序崩溃
【发布时间】:2015-09-25 06:29:26
【问题描述】:

我正在使用 WinJS 开发 WP8.1 应用程序。我正在使用推送通知。我想根据推送通知有效负载中收到的“启动”字符串执行逻辑。我的 toast 通知负载是

<toast launch='launchString'>
   <visual>
      <binding template='ToastText02'>
         <text id='1'>headlineText</text>
         <text id='2'>bodyText</text>
      </binding>
   </visual>
</toast>

我已经注册了一个活动

WinJS.Application.addEventListener("activated", this.onActivated);

定义为

onActivated: function (args) {

  if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
      if (args.detail.arguments) {
          var launchString = args.detail.arguments;
          eval("angular.element(document.getElementById('mainBody')).scope().onNotification(launchString)")

      }
      else {
          console.log("Not launched from toast notification")
      }
  }
}

这里的onNotification(launchString) 函数具有基于launchString 做出某些决策的逻辑。

当应用程序在前台或后台运行时,一切正常。但是当应用程序处于终止状态时,我尝试通过点击 toast 通知来启动,应用程序在启动后立即崩溃。我无法在 VS 中对此进行调试,因为在调试时我无法重新创建此场景(因为调试应用程序被终止时调试器退出)。

  1. 有什么方法可以调试处于终止状态的应用程序?
  2. 当应用程序处于终止状态时,我尝试启动并读取“启动”参数时出现什么问题?我该如何解决这个问题?

感谢您的帮助!

编辑: Here 是一个类似的问题。

【问题讨论】:

  • Visual Studio 调试器不应仅仅因为应用程序退出而退出。事实上,如果有一个未捕获的异常,那应该让你进入调试器。不过,我通常使用 C#,而不是 WinJS。
  • @CBHacking 是的,你是对的。使用 C# 可以,但即使我是 WinJS 应用程序的新手。它只是在没有任何弹出窗口的情况下使应用程序崩溃。
  • 重要安全提示:eval 是邪恶的。除非绝对必须,否则切勿使用它,在这种情况下,您需要非常小心,以确保不受信任的内容无法进入执行线。例如,如果您将 launchString 值连接到您的 eval 参数中,您将允许在您的应用程序中执行任意代码。你不直接运行angular.element(document.getElementById('mainBody')).scope().onNotification(launchString) 有什么原因吗?
  • @CBHacking 我在其中声明 onActivated 函数的 JS 是一个普通的 javascipt 文件。这是我正在初始化和绑定其他内容的文件。我的onNotification 函数在一个角度控制器中,它使用其他角度服务来执行逻辑。因此我不能直接运行angular.element(document.getElementById('mainBody')).scope().onNotification(lau‌​nchString),因为这是纯javascript。
  • here 是一个类似的问题

标签: windows-phone-8.1 winjs toast crash


【解决方案1】:

好的,所以我得到了答案。

onActivated 事件在 cordova deviceready 被触发之前被触发。因此,即使在加载 angular.js 之前,我的代码也会调用 angular。因此,应用程序在前台或后台运行良好,因为 angular.js 已经加载。为了解决这个问题,我使用了超时并检查angular 是否为object

onActivated: function (args) {

  if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
      if (args.detail.arguments) {
          var launchString = args.detail.arguments;

          window.setTimeout(function () {
              if(typeof angular !== undefined)
              eval("angular.element(document.getElementById('mainBody')).scope().onNotificationWindows(launchString)");
          }, 3000);

      }
      else {
          console.log("Not launched from toast notification")
      }
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多