cordova - 推送通知WP8.1 "Invalid channel URL"

标签 cordova windows-phone-8.1 windows-phone visual-studio-2015 wns

我尝试在 WP8.1 上发送推送通知。应用程序是使用 Cordova 构建的。

客户端

  • 我在https://dev.windows.com中创建了一个新的应用程序> 仪表板
  • 通过 Visual Studio 2015 社区,我将我的应用程序与新应用程序关联起来 (右键单击项目>商店>关联)
  • 我的应用身份正常
  • appxmanifest 文件 Toast 功能已选中

在我的代码中(来自 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);
}

我可以从此函数获取通知 URI 并将其存储在我的数据库中。 服务器端 我尝试从 PHP 发送 toast 通知

<?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);

这段代码的输出总是相同的:

"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

有什么想法可以帮助我吗? 谢谢!

最佳答案

您确定在服务器端通过从应用程序收到的正确 channel URL 发送推送通知吗?

您似乎正在尝试向与应用程序无关的 URL 发送通知:

X-WNS-ERROR-DESCRIPTION: Invalid channel URL

确保您将带有应用凭据的正确请求发送至 https://login.live.com/accesstoken.srf 并且您在授权 header (承载)中使用正确的访问 token 。

此外,您的 channel 网址是否始终以 https://db5.notify.windows.com/ 开头 并且您只从数据库中获取 token ?请注意,有时它可以以例如开头。 https://db3.notify.windows.com/

关于cordova - 推送通知WP8.1 "Invalid channel URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33479553/

相关文章:

android - 安装失败 'cordova-plugin-firebase' : CordovaError: Uh oh

android - 在不重新启动应用程序的情况下转移 Android 应用程序开发框架

c# - cortana 调用导致启动时崩溃

wpf - 不使用密码框隐藏 TextBox 控件的文本

c# - 删除 Windows Phone 页面

c# - 固定后更改 TileId?

windows-runtime - Windows Phone 8.1 中的媒体管道是否已损坏?

android - Phonegap - 无法从服务器下载存档

javascript - 未捕获的语法错误 : Invalid or unexpected token when retrieving JSON stringified object from SQLite (Cordova)

c# - 在 WP8.1 中拦截 ManipulationDelta 的问题