From 3667317f775b47fab59020d8b3245c0afd705ef4 Mon Sep 17 00:00:00 2001 From: Raghav Katyal Date: Fri, 10 Jul 2015 12:06:10 -0700 Subject: [PATCH] Moved proxy to windows folder and updated plugin.xml --- plugin.xml | 7 +++ src/windows/PushPluginProxy.js | 86 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/windows/PushPluginProxy.js diff --git a/plugin.xml b/plugin.xml index 2c68c1b7b..9f99a48b7 100755 --- a/plugin.xml +++ b/plugin.xml @@ -84,4 +84,11 @@ + + + + + + + diff --git a/src/windows/PushPluginProxy.js b/src/windows/PushPluginProxy.js new file mode 100644 index 000000000..ca8087b29 --- /dev/null +++ b/src/windows/PushPluginProxy.js @@ -0,0 +1,86 @@ +var myApp = {}; +var pushNotifications = Windows.Networking.PushNotifications; + +var createNotificationJSON = function (e) { + var result = { message: '' }; //Added to identify callback as notification type in the API in case where notification has no message + var notificationPayload; + + switch (e.notificationType) { + case pushNotifications.PushNotificationType.toast: + case pushNotifications.PushNotificationType.tile: + if (e.notificationType === pushNotifications.PushNotificationType.toast) { + notificationPayload = e.toastNotification.content; + } + else { + notificationPayload = e.tileNotification.content; + } + var texts = notificationPayload.getElementsByTagName("text"); + if (texts.length > 1) { + result.title = texts[0].innerText; + result.message = texts[1].innerText; + } + else if(texts.length === 1) { + result.message = texts[0].innerText; + } + var images = notificationPayload.getElementsByTagName("image"); + if (images.length > 0) { + result.image = images[0].getAttribute("src"); + } + var soundFile = notificationPayload.getElementsByTagName("audio"); + if (soundFile.length > 0) { + result.sound = soundFile[0].getAttribute("src"); + } + break; + + case pushNotifications.PushNotificationType.badge: + notificationPayload = e.badgeNotification.content; + result.count = notificationPayload.getElementsByTagName("badge")[0].getAttribute("value"); + break; + + case pushNotifications.PushNotificationType.raw: + result.message = e.rawNotification.content; + break; + } + + result.additionalData = {}; + result.additionalData.pushNotificationReceivedEventArgs = e; + return result; +} + +module.exports = { + init: function (onSuccess, onFail, args) { + + 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); + 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) { + onFail(ex); + } + } +}; +require("cordova/exec/proxy").add("PushNotification", module.exports); + +