Azure/azure-mobile-services-cordova

Please add support for WNS notifications

Closed this issue · 9 comments

Is the WNS support planned? Would be amazing to be able to connect to the Azure Notification Hub and receive the Windows notifs.

TIA

WNS Support is currently in the MobileApp branch for cordova. WNS Support will probably not be added into the Service branch at this time.

xqiu commented

Is the code in https://github.com/Azure/azure-mobile-services/blob/dev/sdk/Javascript/src/Push/Push.WinJS.js ? Should we simply port that code over? At this time, I can't port my cordova app to windows universal due to this restriction.

The I was referring to was here: https://github.com/Azure/azure-mobile-services/tree/MobileApp/sdk/Javascript, but that branch is for Mobile App's and uses a different (newer) API on NH to do registrations so you can't just port it over to a service. (With the cordova files here: https://github.com/Azure/azure-mobile-services-cordova/tree/MobileApp, or on npm)

I don't know if the WInJS version of push would work for WNS on Win Universal, I suppose it should, but it'd be tricky to work in. I think if you want to get this to work on services, I would recommend copying the pattern used for gcm and apns in this web version (https://github.com/Azure/azure-mobile-services/blob/dev/sdk/Javascript/src/Push/Push.Web.js) and just use wns instead as applicable.

xqiu commented

Thanks. As you suggested, I've decided to modify mobileServices.Web-1.2.8.js directly, and I get wns toast to work. The js file is shared here:https://onedrive.live.com/prev?cid=4c9ccfe08b0ea8f6&id=4C9CCFE08B0EA8F6%2147340&v=TextFileEditor . It's not a lot of code to change.

@xqiu did you succeed implementation WNS ? Could you share your your insights?
Thanks so much!

xqiu commented

yes. By using the modified js file linked above.

From the app side in Cordova:

                    if (client) {
                        // Register for notifications.
                        client.push.wns.registerNative(wnsRegId, $scope.getTags()).done(function () {
                            }, function (err) {

                            });
                    }

This is the code I use on the server side.

//insert should use Script/Admin authentication admin, and we use master key for the server.
//read is to use application key... This will ensure that if application key is disclosed, it will not be able to insert to our table.

function insert(item, user, request) {

    // Define a simple payload for a GCM notification.
    var payload = {
        "message": item.title
    };

    // Define a payload for the Windows Store toast notification.
    var payloadwns = '<?xml version="1.0" encoding="utf-8"?><toast><visual>' +
        '<binding template="ToastText01">  <text id="1">' +
        item.title + '</text></binding></visual></toast>';

    request.execute({
        success: function () {
            // If the insert succeeds, send a notification.
            // Send to all template registrations
            push.send(item.tag, payload, {
                success: function (pushResponse) {
                    console.log("Sent push:", pushResponse, payload);

                    // send a notification for windows (a native registration).
                    push.wns.send(item.tag, payloadwns, 'wns/toast', {
                        success: function (pushResponse) {
                            console.log("Sent push wns:", pushResponse);
                            request.respond();
                        },
                        error: function (pushResponse) {
                            console.log("Error Sending push wns:", pushResponse);
                            request.respond(500, { error: pushResponse });
                        }
                    });
                },
                error: function (pushResponse) {
                    console.log("Error Sending push:", pushResponse);
                }
            });
        },
        error: function (err) {
            console.log("request.execute error", err)
            request.respond();
        }
    });
}

@xqiu you could do a pull request with this implementation, this will be very helpful for community.

xqiu commented

Sure. Here it is: #15

@phvannor @xqiu How do you succed to include additional data within wns toast payload?