【问题标题】:how to read device token (parse.com) with phonegap/javascript iOS如何使用 phonegap/javascript iOS 读取设备令牌 (parse.com)
【发布时间】:2014-08-12 09:53:54
【问题描述】:

我正在使用 Phonegap 开发适用于 iOS 的应用程序,并且我正在使用 parse.com 通过 Xcode 中包含的 iOS SDK 进行推送通知。在我的应用程序中,用户可以登录,我需要知道用户的设备令牌以向他们发送单次推送通知,我知道如何在登录期间使用用户/密码发送设备令牌,但我不知道如何阅读它,我不知道是否有任何方法可以直接使用 Phonegap 或者我是否需要使用 C 读取它,然后使用变量传递给 Javascript。

编辑:我正在尝试使用 Malloc 的解决方案,但我遇到了这个问题。我通过 CLI 安装了插件,但我不明白如何使用它。我创建了一个示例页面

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

但我当然会得到一个白页,因为这段代码不打印任何东西。 如果我想将installationid 分配给javascript 变量,我需要做什么? 谢谢

更新 2

现在我的示例页面中有这段代码

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

更新 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>

【问题讨论】:

    标签: javascript ios cordova push-notification parse-platform


    【解决方案1】:

    您需要的是与设备相关的安装 ID。您可能需要为此使用插件。

    可以在here找到我使用过的插件,并且可以按预期工作。

    使用 CLI 安装后,您只需调用 parsePlugin.getInstallationId 函数,该函数将返回您设备的安装 ID。

    请记住,您将在每台设备上获得一个安装 ID,但这并不意味着您始终只有一个。由于您可以在多个设备上运行该应用程序,因此每个设备都有自己的安装 ID。

    更新:

    插件应该在调用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');
        });
    }
    

    【讨论】:

    • 我用我的实际代码更新了我的问题,我删除并再次安装插件但没有任何变化,我仍然有白页
    • console.log() 一起检查的几点: - 检查您的代码是否被调用,并确保它在页面完成加载后运行,就像我更新的代码一样。使用 Javascript 控制台查看是否出现任何错误并在之前修复它们。此外,在您的设备上进行测试之前,请运行 cordova build ios
    • 为了隐私,我从问题中删除了我的 appid 和客户端密钥的一部分
    【解决方案2】:

    您应该使用其他人指出的插件,这是另一个执行相同工作的插件。 https://github.com/ccsoft/cordova-parse

    PS:我是插件的作者。

    【讨论】:

    • 谢谢,我会试试这个。通过 CLI 安装插件后我需要做什么?我需要将 cordovaparse.h 和 cordovaparse.m 添加到 Xcode 项目吗?我需要在 html 标头中包含一些 .js 文件吗?我需要在 html 文件中插入以读取 getInstallationId 的 javascript 代码是什么?谢谢!
    • 您不必执行任何这些操作,请查看有关安装的自述文件。科尔多瓦添加插件为您完成一切。在收到 deviceready(cordova js 事件)后,您可以调用 getInstallationId。我建议您阅读有关插件的 cordova 文档。
    • 谢谢,我阅读了cordova文档并且我理解它,但我不明白我需要如何使用getinstallationid,因为在你的插件页面上只写了“通过成功回调获取Parse安装ID”例如如果我想打印安装ID,我需要使用什么谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2017-04-13
    • 2015-12-02
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多