简体   繁体   English

如何使用phonegap / javascript iOS读取设备令牌(parse.com)

[英]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. 我正在使用Phonegap开发适用于iOS的应用程序,并且使用parse.com通过Xcode随附的iOS SDK进行推送通知。 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. 在我的应用程序中,用户可以登录,我需要知道用户的设备令牌才能向其发送单次推送通知,我知道如何在登录期间随用户/密码一起发送设备令牌,但我不知道如何读取它,我不知道Phonegap是否可以直接使用它,或者我是否需要使用C读取它,然后使用一个变量传递给Javascript。

EDIT: I'm trying to use Malloc's solution, and i have this problem. 编辑:我正在尝试使用Malloc的解决方案,并且我有此问题。 i installed plugin via CLI but i don't understand how to use it. 我通过CLI安装了插件,但我不知道如何使用它。 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? 如果我想将installationid分配给javascript变量,该怎么办? thanks 谢谢

UPDATE 2 更新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 更新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. 您需要的是与设备相关的安装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. 使用CLI安装它之后,您只需调用parsePlugin.getInstallationId函数,该函数将返回设备的安装ID。

Keep in mind that you will get ONE installation id per device but that doesn't mean that you have always only one. 请记住,每个设备将获得一个安装ID,但这并不意味着您始终只有一个。 Since you may run the app on more than a device, each one has his installation id. 由于您可以在多个设备上运行该应用程序,因此每个应用程序都有其安装ID。

Update: 更新:

The plugin should be initialized before calling getInstallationId : 插件应在调用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 https://github.com/ccsoft/cordova-parse

PS: I am the author of the plugin. PS:我是该插件的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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