phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

[Question] Setup for Windows Phone #1244

Open lmmfranco opened 8 years ago

lmmfranco commented 8 years ago

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:

  1. Looking at some other questions here (and the docs) all payloads seem to be XML... is that set in stone? Can't I use the same payload I use for android ?
  2. Is it possible to subscribe to topics? Didn't see any config for this.

Thanks in advance

andreszs commented 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 launchparameter 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.

lmmfranco commented 8 years ago

@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.

lmmfranco commented 8 years ago

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);
macdonst commented 8 years ago

@lmmfranco do you want to do a pull request to add the info to the docs?

andreszs commented 8 years ago

To parse the notification normally from the onActivatedevent, 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
    }
};
daserge commented 7 years ago

Linking to https://github.com/phonegap/phonegap-plugin-push/issues/1033#issuecomment-238237978 covering background/coldStart handling on Windows & WNS.

stale[bot] commented 6 years ago

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.