0
votes

I am trying to implement push notification in JS Metro app. I am registering for push notification and gets a channel URI. If the Channel URI is new I update it on my WCF service which I have created and hosted on my local machine. In my service I first authenticate with WNS and gets the access-token and other details. then I create a request on the Channel URI with access-token in header. In response I get "received" for "badge", "tile" and "toast" notification request I have sent.

But no notification is received in my JS Metro app. Below is the code for registering for push notification and listening for push notification.

var push = Windows.Networking.PushNotifications;
var promise = push.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();
var channel;

function RegisterPNWin() {
try {
    push = Windows.Networking.PushNotifications;
    promise = push.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();

    promise.then(function (ch) {
        channel = ch;
        channel.addEventListener("pushnotificationreceived", notificationReceived);           
        var uri = ch.uri;
        var expiry = ch.expirationTime;

 // here I update Channel URI on my WCF service         
    });
} catch (e) { }
}

function notificationReceived(e, args) {
var notificationTypeName = "";
var notificationPayload;
switch (e.notificationType) {
    // You can get the toast, tile, or badge notification object.
    // In this example, we take the XML from that notification and display it.
    case pushNotifications.PushNotificationType.toast:
        notificationTypeName = "Toast";
        notificationPayload = e.toastNotification.content.getXml();
        break;
    case pushNotifications.PushNotificationType.tile:
        notificationTypeName = "Tile";
        notificationPayload = e.tileNotification.content.getXml();
        break;
    case pushNotifications.PushNotificationType.badge:
        notificationTypeName = "Badge";
        notificationPayload = e.badgeNotification.content.getXml();
        break;
    case pushNotifications.PushNotificationType.raw:
        notificationTypeName = "Raw";
        notificationPayload = e.rawNotification.content;
        break;
}
}

Please let me know if I am missing something. 1. Why this is happening? 2. what is the recommended way to implement for push notification in Windows 8 javascript Metro Apps? 3. do I have to attach a background task to listen to push notification?

any code sample would be great.

thanks.

1
The event that you're listening for will be fired only when the app is active (e.g. on screen) and not suspended. Are you testing it that way? To get the notification when suspended, you need the background task. - Kraig Brockschmidt - MSFT
yes I am testing it when the application is active. - Deeps
A good step is to separate debugging your client from your backend. Can you try your service with the Push and Periodic Notifications client-side sample? (code.msdn.microsoft.com/windowsapps/Push-and-periodic-de225603) I would suggest working with badge notifications for this test because the XML is simplest. Scenarios 1 and 3 are the ones you want. It looks like you pulled some of your code directly from that sample, but using it would eliminate the question of whether the client is doing the right thing. - Kraig Brockschmidt - MSFT

1 Answers

1
votes

Note :- In order to view the push notification support in your project, you need to set the following property, “ToastCapable = Yes” in the “Application” tab of the Package.appxmanifest file.

Hope it may helps you.

Thanks, Joy Oyiess Rex