title: Background task for push notifications on Windows Phone 8.1
body: <p>I'm trying to register a background task on my Windows Phone 8.1 to receive and handle push notifications.At the moment everything is working when the app is opened (foreground+background), but a background task that is defined in the app.js does not work when app is closed.</p>
  
  <p>This is defined in the package.phone.<strong>appxmanifest</strong>:</p>
  
  <pre><code>&lt;Extension Category="windows.backgroundTasks" StartPage="js/lib/backgroundTask.js"&gt;
        &lt;BackgroundTasks&gt;
          &lt;Task Type="pushNotification" /&gt;
        &lt;/BackgroundTasks&gt;
      &lt;/Extension&gt;
  </code></pre>
  
  <p><strong>backgroundTask.js</strong></p>
  
  <pre><code>(function () {
  //var backgroundTask = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current,
  //taskName = backgroundTask.task.name;
  
  Windows.Storage.ApplicationData.current.localSettings.values["hello"] = "world";
  
  close();
  })();
  </code></pre>
  
  <p>this is what my <strong>app.js</strong> does:</p>
  
  <pre><code>  var taskName = "mySuperFancyBgTaskName";
  
    var registerBackgroundTask = function() {
  
      var btr = Windows.ApplicationModel.Background.BackgroundTaskRegistration;
      var iter = btr.allTasks.first();
      var taskRegistered = false;
  
      while (iter.hasCurrent){
          var ta = iter.current.value;
          if (ta.name == taskName){
              taskRegistered = true;
              break;
          }
          iter.moveNext();
      }
  
      if (!taskRegistered){
          var builder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();
          var trigger = new Windows.ApplicationModel.Background.PushNotificationTrigger();
          builder.setTrigger( trigger );
          builder.taskEntryPoint = "js\\lib\\backgroundTask.js";
          builder.name = taskName;
  
          try{
              var task = builder.register();
              //task.addEventListener("completed", onPushNotification);
          }
          catch (e){
              console.error(e);
          }
      }
  }
  
  
  var channel;
  var pushNotificationManager = Windows.Networking.PushNotifications.PushNotificationChannelManager;
  var channelOperation = pushNotificationManager.createPushNotificationChannelForApplicationAsync();
  
  channelOperation.then(function (newChannel) {
      channel = newChannel;
      saveChannelUriInSettings(channel.uri);
      console.log("opened push notification channel with uri: " + channel.uri);
      registerBackgroundTask();
  },
      function (error) {
          console.log("Channel could not be retreived. " + error.number)
      }
  );
  </code></pre>
  
  <p>It seems that backgroundTask.js is never started,because there is nothing written in the localsettings. Tried to do some stuff there, but of course not able to debug there.</p>
  
  <p>If I do</p>
  
  <pre><code>var onPushNotification = function (e) { ...}
  channel.addEventListener("pushnotificationreceived", onPushNotification);
  </code></pre>
  
  <p>receiving raw push notifications works fine. So how do I get backgroundtask to work, so that it can save incoming push notifications? If it is working there is no need to define a event listener in the app to catch push notifications, right?</p>
  
  <p>Any help is appreciated - thanks in advance!</p>