How to avoid duplicate files using Parse?

I noticed that every time I deployed my application to my phone, it duplicates the installation in parsing to receive push notifications. How can I avoid this when I reinstall the application?

+3
source share
1 answer

After a week of research and trial and error, I finally found a working solution. Basically, for this you need to do two things:

  • In your Android app: pass in a unique identifier when initializing and saving ParseInstallation. we will use ANDROID_ID .
  • Parse Cloud Code:. Installation, , 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. - . , .
  • , put("androidId", android_id); androidId Installation Parse App.

: [1], [2], [3]

+3

Source: https://habr.com/ru/post/1546890/


All Articles