在 Windows Phone 8.1 中未收到 WNS

Not receiving WNS in Windows Phone 8.1

我正在 Windows Phone 8.1(非 silverlight)中开发一个应用程序,它使用 WNS 接收原始推送通知。

虽然我通过 Visual Studio 运行 我的应用程序(通过物理设备,而不是在模拟器中),但我总是收到推送通知(应用程序在前台,后台,phone 已锁定...) 所以我认为我接收推送的代码是正确的。

我的问题是我 运行 我的应用程序没有使用 Visual Studio。如果我在设备上按 "app icon" 以启动应用程序并将其保持在前台,我会收到推送通知。但是,如果我转到 phone 菜单,或锁定设备(不关闭应用程序),我不会收到推送通知,但服务器会说 WNS 已成功发送。如果我再次将应用程序置于前台,我将再次收到推送通知...

所以,总结:通过Visual Studio初始化应用程序,我总是收到 WNS 通知。通过设备初始化应用程序,仅在应用程序处于前台时接收 WNS。服务器总是成功发送WNS。

这是我接收 WNS 的代码:

public static void OnPushNotificationReceived(PushNotificationChannel channel, PushNotificationReceivedEventArgs e)
{
    Debug.WriteLine("Received WNS notification");

    string notificationContent = String.Empty;
    switch (e.NotificationType)
    {
        case PushNotificationType.Badge:
            Debug.WriteLine("Badge notifications not allowed");
            return;
        case PushNotificationType.Tile:
            Debug.WriteLine("Tile notifications not allowed");
            return;
        case PushNotificationType.Toast:
            Debug.WriteLine("Toast notifications not allowed");
            return;
        case PushNotificationType.Raw:
            notificationContent = e.RawNotification.Content;
            break;
    }

    processWnsNotification(notificationContent);
}

//Show local toast notification
private static void processWnsNotification(string notification)
{
    ToastTemplateType toastTemplateXml = ToastTemplateType.ToastText01;
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplateXml);

    XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
    toastTextElements[0].AppendChild(toastXml.CreateTextNode("New message"));

    ToastNotification toast = new ToastNotification(toastXml);

    if(toastNotifier == null)
    {
        toastNotifier = ToastNotificationManager.CreateToastNotifier();
    }
    toastNotifier.Show(toast);
}

有人知道发生了什么事或我做错了什么吗? 我认为这可能是配置问题...但我一直在深入研究 属性 和配置文件但没有成功。

非常感谢!! 乔治。

这是预期的行为并且有很好的记录,您的应用程序将仅在未暂停时或在注册可以处理原始通知的 background task 时接收原始推送通知。

当 Visual Studio 打开时您会收到它们,因为当附加调试器时应用程序不会暂停。要调试应用程序的暂停,请转至 View->Toolbars 并启用 Debug Location 工具栏,然后启动您的应用程序,然后 select Lifecycle Events 中的 Suspend 选项盒子。你会注意到你的代码没有执行(用断点测试)。

您的服务器可能会发送通知,但您的 phone 不会收到通知。您可能不检查推送请求的响应,但应该有一些指示表明客户端不可用或类似的东西(状态代码表明可能)。