0
votes

I am developing WP8.1 app with WinJS. I am using push notifications. I want to execute a logic depending on "launch" string received in push notification payload. My toast notification payload is

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

I have registered an event

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

Which is defined as

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")
      }
  }
}

Here onNotification(launchString) function has the logic which makes some decision making based on launchString.

Everything works fine when application is either in foreground or running in background. But when the application is in killed state, and I try to launch by tapping on toast notification, application crashes right after its launched. I am not able to debug this in VS as I could not recreate this scenario while debugging (because debugger exits when a debugging app is killed).

  1. Is there any way I can debug an app in killed state?
  2. What is the issue when I try to launch and read "launch" parameter when app is in killed state? How can I solve this?

Thanks for your help!

Edit: Here is a similar issue.

1
The Visual Studio debugger should not exit just because the app does. In fact, if there's an uncaught exception, that ought to drop you into the debugger. I usually work with C#, though, not WinJS. - CBHacking
@CBHacking yes you're right. With C# it does, but even I am new to WinJS apps. It just crashes application without any popups. - Sharvil
Important security note: eval is evil. Never use it unless you absolutely must, and in that case you need to be extremely careful to be sure that there's no way for untrusted content to get into the execution line. If you were, for example, concatenating the launchString value into your eval parameter, you would be allowing arbitrary code execution in your app. Is there some reason you aren't just running angular.element(document.getElementById('mainBody')).scope().onNotification(launchString) directly? - CBHacking
@CBHacking The JS in which I have declared onActivated function is a plain javascipt file. This is the file where I am initializing and binding other stuff. My onNotification function is in an angular controller which uses other angular services to execute a logic. Hence I could not directly run angular.element(document.getElementById('mainBody')).scope().onNotification(lau‌​nchString) as this is in plain javascript. - Sharvil
here is a similar issue - Sharvil

1 Answers

0
votes

Ok, so I got my answer.

onActivated event was getting fired before cordova deviceready was fired. Hence my code was making a call angular even before angular.js was loaded. Hence application worked fine when it was in foreground or in background as angular.js was already loaded. To solve this I have used a timeout and checked if angular is an 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")
      }
  }

}