【问题标题】:How to avoid Installation duplicates using Parse?如何使用 Parse 避免安装重复?
【发布时间】:2015-01-20 16:57:31
【问题描述】:

我注意到每次我将我的应用程序部署到我的手机时,它都会在解析中复制安装以接收推送通知。重新安装应用时如何避免这种情况发生?

【问题讨论】:

  • 我们需要更多信息。包名是什么?你在使用 Gradle 吗?如果是这样,请发布相关的 build.gradle 文件。
  • 我正在使用 Eclipse IDE
  • 仍然需要更多信息...“复制安装”是什么意思?重复申请?那么另一个packageName
  • 我编辑了我的帖子,通过安装我指的是 Parse API。不是应用程序本身安装。
  • 可能是因为每次重新安装都有不同的 hashkey,Parse 用来知道安装和配置是相同的,当这个条件不相等时,Parse API 使新安装不重复。

标签: android parse-platform installation


【解决方案1】:

经过一周的研究和反复尝试,我终于找到了一个可行的解决方案。基本上,你需要做两件事来实现这一点:

  • 在您的 Android 应用中:在初始化并保存 ParseInstallation 时传递一些唯一 ID。我们将使用ANDROID_ID
  • 在 Parse Cloud 代码中: 在保存新的 Installation 之前,请检查其唯一 ID 是否存在于旧的 Installation 中。如果是,请删除旧的。

怎么做:

  • 在您的应用程序的onCreate() 方法中:

    //First: get the ANDROID_ID
    String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
    Parse.initialize(this, "APP_ID", "CLIENT_KEY");
    //Now: add ANDROID_ID value to your Installation before saving.
    ParseInstallation.getCurrentInstallation().put("androidId", android_id);
    ParseInstallation.getCurrentInstallation().saveInBackground();
    
  • 在您的云代码中,添加以下内容:

    Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
        Parse.Cloud.useMasterKey();
        var androidId = request.object.get("androidId");
        if (androidId == null || androidId == "") {
            console.warn("No androidId found, save and exit");
            response.success();
        }
        var query = new Parse.Query(Parse.Installation);
        query.equalTo("androidId", androidId);
        query.addAscending("createdAt");
        query.find().then(function(results) {
            for (var i = 0; i < results.length; ++i) {
                console.warn("iterating over Installations with androidId= "+ androidId);
                if (results[i].get("installationId") != request.object.get("installationId")) {
                    console.warn("Installation["+i+"] and the request have different installationId values. Try to delete. [installationId:" + results[i].get("installationId") + "]");
                    results[i].destroy().then(function() {
                        console.warn("Installation["+i+"] has been deleted");
                    },
                    function() {
                        console.warn("Error: Installation["+i+"] could not be deleted");
                    });
                } else {
                    console.warn("Installation["+i+"] and the request has the same installationId value. Ignore. [installationId:" + results[i].get("installationId") + "]");
                }
            }
            console.warn("Finished iterating over Installations. A new Installation will be saved now...");
            response.success();
        },
        function(error) {
            response.error("Error: Can't query for Installation objects.");
        });
    });
    

就是这样!


您可能想知道的事情:

  • Android 设备没有完美唯一标识符。就我而言,我使用了ANDROID_ID。你可能会用别的东西。 Read this official article 以获得更好的照片。
  • 请注意,在第一次调用 put("androidId", android_id); 后,一个名为 androidId 的新列将添加到 Parse App Dashboard 中的 Installation 表视图中。

资源:[1]、[2]、[3]

【讨论】:

    【解决方案2】:

    对于任何尝试使用 React Native(和 Expo)的人...我已经编写了这个例程来帮助解决安装重复问题。它纯粹是客户端,但似乎可以很好地与 Cloud Code 重复检查结合使用。

    import Parse from 'parse/react-native.js';
    import { Platform } from 'react-native';
    import * as Application from 'expo-application';
    import * as Localization from 'expo-localization';
    
    export const currentInstallationAsync = async (): Parse.Installation => {
        const installationId = await Parse._getInstallationId();
        console.log(`installationId: ${installationId}`);
    
        return new Parse.Installation({
            installationId,
            appName: Application.applicationName,
            appVersion: Application.nativeApplicationVersion,
            deviceType: Platform.OS,
            localeIdentifier: Localization.locale,
            timeZone: Localization.timezone,
            appIdentifier: Application.applicationId,
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2016-04-08
      • 1970-01-01
      相关资源
      最近更新 更多