2

I try to send push notifications on WP8.1. App is build with Cordova.

Client side

  • I create a new new app in https://dev.windows.com > Dashboard
  • With visual studio 2015 community, I associate my app to the new app (right click on project> Store > Associate)
  • My app identity is ok in
  • appxmanifest file Toast Capable is checked

In my code (from https://github.com/phonegap/phonegap-plugin-push):

try {   
    pushNotifications.PushNotificationChannelManager.
        createPushNotificationChannelForApplicationAsync().done(
    function (channel) {
        var result = {};
        result.registrationId = channel.uri;
        myApp.channel = channel;
        channel.addEventListener("pushnotificationreceived",
            onNotificationReceived);
        myApp.notificationEvent = onNotificationReceived;
        onSuccess(result, { keepCallback: true });
    }, function (error) {
        onFail(error);
    });
} catch (ex) {
    onFail(ex);
}

I can get the notification URI from this function and store it in my database. Server side I try to send a toast notification from a PHP

<?php
    $sid = urlencode("ms-app://s-1-15-2-xxxxxxxxx");
    $secret = urlencode("mysecret");
    $uri = 'https://db5.notify.windows.com/?token=xxx';//From database
    $str = "grant_type=client_credentials&client_id=$sid&
        client_secret=$secret&scope=notify.windows.com";
    $url = "https://login.live.com/accesstoken.srf";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
        array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$str");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output);
    var_dump($output);
    if(isset($output->error)){
        throw new Exception($output->error_description);
    }
    $accessToken = $output->access_token;
    $sendPush = curl_init();
    curl_setopt($sendPush, CURLOPT_URL, $uri);
    //TOAST MESSAGE
    $toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
        "<wp:Notification xmlns:wp=\"WPNotification\">" .
        "<wp:Toast>" .
        "<wp:Text1>Text...</wp:Text1>" .
        "<wp:Text2>text..</wp:Text2>" .
        "</wp:Toast>" .
        "</wp:Notification>";
    curl_setopt($sendPush, CURLOPT_HEADER, true);
    $headers = array('Content-Type: text/xml',"Content-Type: text/xml",
        "X-WNS-RequestForStatus:1", "X-WNS-Type: wns/toast",
        "Content-Length: " . strlen($toastMessage),
        "X-NotificationClass:2" ,
        "X-WindowsPhone-Target: toast","Authorization: Bearer $accessToken");
    curl_setopt($sendPush, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($sendPush, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($sendPush,CURLOPT_POST, true);
    curl_setopt($sendPush, CURLOPT_POSTFIELDS, $toastMessage);
    $output = curl_exec($sendPush);
    var_dump($output);
    curl_close($sendPush);

The output of this peace of code is always the same :

"HTTP/1.1 404 Not Found
Content-Length: 0
X-WNS-NOTIFICATIONSTATUS: dropped
X-WNS-STATUS: dropped
X-WNS-ERROR-DESCRIPTION: Invalid channel URL
X-WNS-MSG-ID: 6A7ABBBB65E14731
X-WNS-DEBUG-TRACE: DB5SCH101122042
Strict-Transport-Security: max-age=31536000; includeSubDomains
Date: Mon, 02 Nov 2015 13:11:26 GMT

Any ideas to help me? Thanks !

grandv22
  • 108
  • 7

1 Answers1

2

Are you sure, on server side, you are sending push notification on proper channel URL received from your application?

It looks like you are trying to send notification to URL not related with application:

X-WNS-ERROR-DESCRIPTION: Invalid channel URL

Ensure you are sending proper request with your app credentials to https://login.live.com/accesstoken.srf and you are using proper access token in Authorization header (Bearer).

Additionally, does your channel URL always start with https://db5.notify.windows.com/ and you get only token from database? Be aware, sometimes it can start with eg. https://db3.notify.windows.com/

marcinax
  • 1,067
  • 7
  • 10
  • Thanks for your reply. You're right. I had to encode the 'token' part of the uri to make it work – grandv22 Nov 02 '15 at 15:53
  • @marcinax I tried with the WNS java library to send the push notification to windows phone 8.1.I am getting the same error Invalid channel URL.As you mentioned in the above answer.I am not getting the channel URL starts with either https://db5.notify.windows.com/ or https://db3.notify.windows.com/.The subscription channel URL I got started with https://hk2.notify.windows.com/?token="*************" like this .Please help me for this. – bhadram Jan 19 '16 at 10:50