我試圖在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是你想要的。它看起來像是直接從樣本中抽取了一些代碼,但使用它會消除客戶是否做正確的事情的問題。 –