Open lmmfranco opened 8 years ago
Regarding the server-side payload (what you send to GCM and WNS) here are a few tips:
Here is the basic XML payload for a WNS toast push notification:
<toast>
<visual>
<binding template="ToastText02">
<text id="1">headlineText</text>
<text id="2">bodyText</text>
</binding>
</visual>
</toast>
If you want to send additional parameters (to prevent another push just to send RAW data), modify the toast node to incude the launch
parameter with your JSON data:
<toast launch="(additional JSON encoded data can go here)">
Unfortunately, the push plugin persists in ignoring this data (unless the app was closed/unloaded) There is a workaround to read the data always though, I'll post it if you need it.
All WNS payload templates are shown below, but notice that not all of them are designed for WP8: https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx
Also, remember that WNS requires that you set proper headers to your POST request:
$headers = array(
'Content-Type: text/xml',
'X-WNS-Type: wns/toast', /* means that push notification is a toast */
'Authorization: Bearer ' . $myWnsToken, /* here goes the regid you got from the push.on('registration') method in your app */
'Content-Length: ' . strlen($myNotificationXml) /* I don't use this header and it works */
);
Last, ensure you are reading and saving the result of the push when sent to WNS, you'll find descriptions of any errors there. I had to struggle with the token because Javascript converted some HTML entities to normal chars (such as + or /) and you cannot send them un-encoded; WNS will reject the token unless you sent the token exactly as received).
Regarding the payload on the client side, you just receive JSON objects. There is an extra object in WP8, the data.additionalData.pushNotificationReceivedEventArgs
object, that is not well documented but I already found out how to deal with it.
The plugin is very selective and you'll find out that basic params such as data.additionalData.foreground
are undefined on the Windows implementation.
@andreszs Thanks for posting such detailed reply. It was very helpful! :)
Unfortunately, the push plugin persists in ignoring this data (unless the app was closed/unloaded) There is a workaround to read the data always though, I'll post it if you need it.
Could you post the workaround you mentioned? I guess it will probably be better than sending two requests for every device that needs to be notified.
Got it working perfectly! Once more, thank you @andreszs!
For anyone reading this question in the future with the same issue, you can receive the launch args by registering the activated
event with cordova.
var onActivated = function (launchArguments) {
var args = launchArguments.args;
};
document.addEventListener('activated', onActivated, false);
@lmmfranco do you want to do a pull request to add the info to the docs?
To parse the notification normally from the onActivated
event, I have expanded @lmmfranco 's function like this:
/* Windows-only event */
function onActivated(launchArguments) {
var args = launchArguments.args;
if(args != undefined && args != '') {
// Process push notification
var data = {};
data.launchArgs = args;
data.additionalData = {};
data.additionalData.coldstart = false;
data.additionalData.foreground = false;
handleNotification(data); // handle notification normally
}
};
Linking to https://github.com/phonegap/phonegap-plugin-push/issues/1033#issuecomment-238237978 covering background/coldStart handling on Windows & WNS.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I'm trying to setup this plugin for windows phone and couldn't get it to work yet.
I'd appreciate if anyone can answer those questions:
Thanks in advance