我试图在JS Metro应用程序中实现推送通知。我正在注册推送通知并获取通道URI。如果Channel URI是新的,我在我的本地机器上创建和托管的WCF服务上进行更新。 在我的服务中,我首先与WNS进行身份验证并获取访问令牌和其他详细信息。然后我在Channel头部使用access-token创建一个请求。作为回应,我收到我收到的“徽章”,“瓦片”和“吐司”通知请求。推送通知不在JS地铁应用程序中接收
但我的JS Metro应用程序没有收到通知。以下是注册推送通知和侦听推送通知的代码。
var push = Windows.Networking.PushNotifications;
var promise = push.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();
var channel;
function RegisterPNWin() {
try {
push = Windows.Networking.PushNotifications;
promise = push.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();
promise.then(function (ch) {
channel = ch;
channel.addEventListener("pushnotificationreceived", notificationReceived);
var uri = ch.uri;
var expiry = ch.expirationTime;
// here I update Channel URI on my WCF service
});
} catch (e) { }
}
function notificationReceived(e, args) {
var notificationTypeName = "";
var notificationPayload;
switch (e.notificationType) {
// You can get the toast, tile, or badge notification object.
// In this example, we take the XML from that notification and display it.
case pushNotifications.PushNotificationType.toast:
notificationTypeName = "Toast";
notificationPayload = e.toastNotification.content.getXml();
break;
case pushNotifications.PushNotificationType.tile:
notificationTypeName = "Tile";
notificationPayload = e.tileNotification.content.getXml();
break;
case pushNotifications.PushNotificationType.badge:
notificationTypeName = "Badge";
notificationPayload = e.badgeNotification.content.getXml();
break;
case pushNotifications.PushNotificationType.raw:
notificationTypeName = "Raw";
notificationPayload = e.rawNotification.content;
break;
}
}
请让我知道如果我失去了一些东西。 1.为什么会发生这种情况? 2.在Windows 8 JavaScript Metro应用程序中实施推送通知的推荐方式是什么? 3.我是否需要附加后台任务才能收听推送通知?
任何代码示例都会很棒。
谢谢。
觉得你认真倾听只有当应用程序被激活将被解雇(如在屏幕上),而不是挂起的事件。你正在测试它吗?要在暂停时获得通知,您需要后台任务。 –
是的,我在应用程序处于活动状态时正在测试它。 – Deeps
一个好的步骤是将你的客户端与你的后端分开调试。您能否使用Push和Periodic Notifications客户端示例尝试您的服务? (http://code.msdn.microsoft.com/windowsapps/Push-and-periodic-de225603)因为XML最简单,所以我建议使用这个测试的徽章通知。方案1和3是你想要的。它看起来像是直接从样本中抽取了一些代码,但使用它会消除客户是否做正确的事情的问题。 –