Windows/Phone 8.1–Windows Phone 8.1 Emulator + Simulated Notifications

In a similar way to having a common way of doing background tasks across Windows/Phone 8.1 I wanted to have a brief play with push notifications on Windows/Phone 8.1 because for a little while I’ve been noticing this new “simulator” in the new Windows Phone 8.1 emulator and I’ve been wanting to try it out.

image

A way to simulate push notifications into an app would be a very cool thing to have – we certainly don’t have this on Windows 8.1 – and so I thought I’d try it out with a “Hello World”.

First off, I made sure that the app could display toast notifications by changing its manifest;

image

and then I added a button on to the main page and wrote this line of code behind it ( the same line of code that I’d use on Windows 8.1 to get a notification URI for the application’s “main” notification channel );

      var channel = 
        await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

      channel.PushNotificationReceived += (s, e) =>
        {
          Debug.WriteLine("Push notification");
        };

then I ticked the little button at the top of the emulator’s dialog;

image

and then I ran my code followed by clicking the “refresh” button in the emulator’s dialog;

image

and as if by magic the dialog sprang into life (I’ve never seen this dialog do anything before today so I was quite excited Smile) and, pretty quickly, I’d faked up a toast notification;

image

and sent it into my app and, lo and behold,

it didn’t work Smile

this was a major disappointment. I tried a few things (quite a few things) before I realised that I hadn’t really followed the instructions on the screen properly.

The next time around I did;

  1. Debug the app.
  2. Enable the notifications simulator on the emulator.
  3. Stop debugging the app.
  4. Debug the app again.
  5. Click the button in the app to request a channel URI.
  6. Click the “Refresh” button in the emulator and see my URI appear in the drop-down.
  7. Choose a toast notification template.
  8. Click “Send”.

and…

it worked a treat Smile

here’s my notification arriving.

 

image

and my debug output window in visual studio was dumping out the right debug log as the push notification came in;

image

This is a really cool thing to have in the tools – I’d love to see this for Windows 8.1 apps as well although I appreciate that Windows 8.1 doesn’t have an emulator, it has a simulator.

Now, in the previous post I wrote a background task which was triggered off a time zone change trigger.

I wondered – what if I changed the task registration that I was using to make use of a push notification trigger? (NB: this is only possible for raw push notifications).

I re-opened that project and changed the registration code such that it did 2 things;

  1. Asked for a push notification channel URI.
  2. Registered the background task to hang off a push notification trigger.

as in;

      // I'm not going to use the channel returned directly here in code so
      // ignoring the return value.
      await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

      BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
      taskBuilder.Name = "MyBackgroundTask";

      // Changed the trigger type here to be a push notification trigger.
      PushNotificationTrigger trigger = new PushNotificationTrigger();
      taskBuilder.SetTrigger(trigger);
      taskBuilder.TaskEntryPoint = typeof(TheBackgroundTask.TheTask).FullName;
      taskBuilder.Register();

and I updated my manifest to switch from specifying a system trigger to a push notification trigger;

image

and I gave it a try on the emulator but it didn’t work;

image

and I spent quite a while staring at it until I realised that in writing the code for the previous post I’d missed out a step in moving some code from one project into another. I’d forgotten to request permission to run background tasks. As per;

http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.backgroundexecutionmanager.requestaccessasync.aspx

and so I changed that registration code again;

      await BackgroundExecutionManager.RequestAccessAsync();

      await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

      BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
      taskBuilder.Name = "MyBackgroundTask";

      // Changed the trigger type here to be a push notification trigger.
      PushNotificationTrigger trigger = new PushNotificationTrigger();
      taskBuilder.SetTrigger(trigger);
      taskBuilder.TaskEntryPoint = typeof(TheBackgroundTask.TheTask).FullName;
      taskBuilder.Register();

and that seemed to resolve the issue for me. That is, the screenshot below;

image

shows my little application running and I’ve simulated a raw push notification (up to 5K of data if I remember correctly) arriving from the cloud into the app and a background task has run in response to that data arriving and because the foreground app happened to be running it’s fired events “into” the foreground app which has picked them up and is changing its UI in response.

I like that simulator a lot Smile