在 Windows Phone 8.1 中未接收 WNS [英] Not receiving WNS in Windows Phone 8.1

查看:74
本文介绍了在 Windows Phone 8.1 中未接收 WNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an app in Windows Phone 8.1 (NOT silverlight) which uses WNS to receive raw push notification.

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

While I run my app through Visual Studio (over a physical device, not in the emulator), I always receive push notifications (with the app in foreground, in background, with the phone locked...) so I think my code to receive push is correct.

我的问题是我在不使用 Visual Studio 的情况下运行我的应用程序.如果我按下设备上的应用程序图标"来初始化应用程序并将其保持在前台,我会收到推送通知.但是如果我进入电话菜单,或者锁定设备(不杀死应用程序),我没有收到推送通知,但服务器说WNS已成功发送.如果我再次将应用程序置于前台,我将再次收到推送通知...

My issue is when I run my app whithout using Visual Studio. If I press the "app icon" on the device to init the app and keep it in foreground, I receive push notifications. But if I go to the phone menu, or lock the device (without killing the app), I don't receive push notifications, but the server says that WNS has been sent successfully. If I put the app again into foreground, I receive push notifications again...

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

So, summarizing: Init app through Visual Studio, I receive always the WNS notifications. Init app through device, only receive WNS with the app in foreground. The server always send the WNS successfully.

这是我接收 WNS 的代码:

Here is my code to receive 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);
}

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

Anyone know what happens or what I'm doing wrong? I think it could be a configuration issue... but I've been digging into property and config files without success.

非常感谢!!乔治.

推荐答案

这是预期的行为,并且有很好的文档记录,您的应用仅在未挂起或 后台任务时才会收到原始推送通知 已注册,可以处理原始通知.

This is the expected behavior and is very well documented, that your app will receive raw push notifications only when it is not suspended or when a background task is registered which can handle raw notifications.

您会在 Visual Studio 打开时收到它们,因为当附加调试器时,应用程序不会挂起.要调试应用的暂停,请转到 View->Toolbars 并启用 Debug Location 工具栏,然后启动您的应用,并选择 SuspendLifecycle Events 框中的 code> 选项.您会注意到您的代码没有执行(使用断点测试).

You receive them when Visual Studio is open because when the debugger is attached the app does not suspend. To debug the suspension of an app, go to View->Toolbars and enable the Debug Location toolbar, then start your app, and select the Suspend option from the Lifecycle Events box. You will notice that your code does not execute (test with breakpoint).

通知可能由您的服务器发送,但您的手机不会收到.您可能不会检查推送请求的响应,但应该有一些迹象表明客户端不可用或类似(状态代码表明可能).

The notification may be sent by your server, but will not be received by your phone. You probably don't check the responses of your push requests, but there should be some indication that the client is not available or something similar (a status code that states that maybe).

这篇关于在 Windows Phone 8.1 中未接收 WNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆