Search code examples
javascriptparse-platformwinjsuwp

Create Parse installation object UWP winjs


I'm using Parse javascript SDK in my UWP javascript application. As mentioned in the quickstart, I'm initializing Parse with following line,

Parse.initialize("App_ID", "Javascript_Key");

but it does not create installation object in Parse dashboard. Also, as mentioned in docs,

Installation data can only be modified by the client SDKs, the data browser, or the REST API.

I'm trying to follow some answer to create installation object, here, https://stackoverflow.com/a/32599778 but I'm not able to get it working. Any ideas on how to create installation object? Thanks


Solution

  • You'll first need to generate a "native" installation id for your device. You can probably use the one already generated by the Parse javascript platform, but it's not hard to generate a new one. Just mimic what's already been done in the Parse source code:

    function generateInstallationId(){
        function hexOctet() {
            return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
        }
    
        return hexOctet() + hexOctet() + '-' + hexOctet() + '-' + hexOctet() + '-' + hexOctet() + '-' + hexOctet() + hexOctet() + hexOctet();
    }
    

    Then construct the proper xhr headers. Since we are mimicking the way a native Parse SDK uses REST, we use the dotNet key or the client key, not the javascript key.

    var xhrHeaders = {
        "Content-Type": "application/json",
        "X-Parse-Application-Id": YOUR_APP_ID,
        "X-Parse-Windows-Key": YOUR_DOTNET_KEY
    };
    

    Then construct your installation object data so that it would pass server validation.

    var installationData = {
       appIdentifier: "your.app.package",
       appName: "appName",
       appVersion: "1.0.0",
    
       deviceType: "winrt",
       deviceUris: {_Default: YOUR_WNS_CHANNEL_URI},
       timeZone: "America/Los_Angeles",
       localeIdentifier: "en-US",
       parseVersion: "1.7.0.0",
       installationId: generateInstallationId()
    }
    

    Finally, make the REST call

    WinJS.xhr({
        type: "POST",
        url: "https://api.parse.com/1/installations",
        headers: xhrHeaders,
        data: JSON.stringify(installationData)
    });
    

    I plucked most of this code from parse-push-plugin, particularly this file. If you need more context or want to see how the WNS channel is obtained, feel free to look there.