diff --git a/src/windows8/PushPluginProxy.js b/src/windows8/PushPluginProxy.js index efe917889..cddbee185 100644 --- a/src/windows8/PushPluginProxy.js +++ b/src/windows8/PushPluginProxy.js @@ -1,18 +1,67 @@ -// Copyright (c) Microsoft Open Technologies, Inc. Licensed under the MIT license. +var myApp = {}; +var pushNotifications = Windows.Networking.PushNotifications; + +var createNotificationJSON = function (e) { + var result = {}; + var notificationPayload; + + switch (e.notificationType) { + case pushNotifications.PushNotificationType.toast: + notificationPayload = e.toastNotification.content.getXml(); + break; + + case pushNotifications.PushNotificationType.tile: + notificationPayload = e.tileNotification.content.getXml(); + break; + + case pushNotifications.PushNotificationType.badge: + notificationPayload = e.badgeNotification.content.getXml(); + break; + + case pushNotifications.PushNotificationType.raw: + notificationPayload = e.rawNotification.content; + break; + } + result.message = ""; + result.xmlContent = notificationPayload; + result.objectReference = e; + return result; +} module.exports = { - register: function (success, fail, args) { - try { - var onNotificationReceived = window[args[0].ecb]; + init: function (onSuccess, onFail, args) { - Windows.Networking.PushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync().then( + var onNotificationReceived = function (e) { + var result = createNotificationJSON(e); + onSuccess(result, { keepCallback: true }); + } + + try { + pushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync().done( function (channel) { + var result = {}; + result.registrationId = channel.uri; + myApp.channel = channel; channel.addEventListener("pushnotificationreceived", onNotificationReceived); - success(channel); - }, fail); + myApp.notificationEvent = onNotificationReceived; + onSuccess(result, { keepCallback: true }); + }, function (error) { + onFail(error); + }); + } catch (ex) { + onFail(ex); + } + }, + unregister: function (onSuccess, onFail, args) { + try { + myApp.channel.removeEventListener("pushnotificationreceived", myApp.notificationEvent); + myApp.channel.close(); + onSuccess(); } catch(ex) { - fail(ex); + onFail(ex); } } }; -require("cordova/windows8/commandProxy").add("PushPlugin", module.exports); \ No newline at end of file +require("cordova/exec/proxy").add("PushNotification", module.exports); + +