问题
I'm developing an app in Windows Phone 8.1 (NOT silverlight) which uses WNS to receive raw push notification.
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.
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...
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.
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.
Thanks a lot!! Jorge.
回答1:
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.
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).
来源:https://stackoverflow.com/questions/33674447/not-receiving-wns-in-windows-phone-8-1