简体   繁体   中英

how to read device token (parse.com) with phonegap/javascript iOS

I'm developing an app for iOS with Phonegap and I'm using parse.com for push notification with iOS SDK included with Xcode. In my app users can login and I need to know the device token of the users to send to them single push notification, I know how to send the device token with the user/password during log in but I don't know how to read it, I don't know if there is any way directly with Phonegap or if I need to read it with C and then pass to Javascript with a variable.

EDIT: I'm trying to use Malloc's solution, and i have this problem. i installed plugin via CLI but i don't understand how to use it. I create a sample page

<html> 
<head> 
</head> 
<body> 
<script type="text/javascript> parsePlugin.getInstallationId(function(id) { document.write(id); }, function(e) { alert('error'); }); 
</script> 
</body> 
</html>

but of course i obtain a white page because this code don't print anything. What i need to do if i want to assign installationid to a javascript variable? thanks

UPDATE 2

Now I have this code in my sample page

<html> 
<head> 
</head> 
<body> 
<script type="text/javascript">

     var appId = p3Z10Nj2GdmfuxCX1a9liu5VlPadbvgW********;
    var clientKey = lFRl93OEDM0bFTVOKaA1iIXV4vuWS3S9********;
    //
    parsePlugin.initialize(
    appId,
    clientKey,
    function() {

    // The parse plugin is initialized correctly, query the installation id
    parsePlugin.getInstallationId(
        function(id) {
                // Installation id is correctly queried
                console.log('The installation id is: '+id);
        },
        function(e) {
                alert('An error has occured while querying the installation id of your device');
    });
    },
    function(e) {
        alert('An error has occured while initializing the parse plugin');
});

</script> 
</body> 
</html> 

UPDATE 3

<html> 
<head> 
</head> 
<body> 
<script type="text/javascript">


 $(window).load(function() {
     var appId = p3Z10Nj2gm8siK;
    var clientKey = lFRl93OEDM0bFTVOKaA1iIXV4vu;
        //
        parsePlugin.initialize(
        appId,
        clientKey,
        function() {

        // The parse plugin is initialized correctly, query the installation id
        parsePlugin.getInstallationId(
            function(id) {
                    // Installation id is correctly queried
                    console.log('The installation id is: '+id);
            },
            function(e) {
                    alert('An error has occured while querying the installation id of your device');
        });
        },
        function(e) {
            alert('An error has occured while initializing the parse plugin');
    });
}
</script> 
</body> 
</html>

What you need is the device related installation id. You may need to use a plugin for this.

The plugin I have used, and which worked as expected, can be found here .

After installing it with the CLI, you just call the parsePlugin.getInstallationId function which will return the installation id for your device.

Keep in mind that you will get ONE installation id per device but that doesn't mean that you have always only one. Since you may run the app on more than a device, each one has his installation id.

Update:

The plugin should be initialized before calling getInstallationId :

$(window).load(function() {
        var appId = YOUR_APP_ID;
        var clientKey = YOUR_KEY_CLIENT;
        //
        parsePlugin.initialize(
        appId,
        clientKey,
        function() {

        // The parse plugin is initialized correctly, query the installation id
        parsePlugin.getInstallationId(
            function(id) {
                    // Installation id is correctly queried
                    console.log('The installation id is: '+id);
            },
            function(e) {
                    alert('An error has occured while querying the installation id of your device');
        });
        },
        function(e) {
            alert('An error has occured while initializing the parse plugin');
    });
}

You should be using a plugin as others pointed out, here is another plugin that does the same job. https://github.com/ccsoft/cordova-parse

PS: I am the author of the plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM