Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android cannot delete parse installation

I use parse to handle push notification. Because I already have my own database, I use parse to store installation data only.(Not using ParseUser to login and logout in the app) When I logout my app, I would like to delete my installation.

ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.deleteInBackground(new DeleteCallback() {
    @Override
    public void done(ParseException ex) {
        Log.d(TAG, "ParseInstallation deleteInBackground done");
        if (ex != null) {
            Log.e(TAG, "ParseInstallation deleteInBackground", ex);
        }
    }
});

Then I got the following error:

com.parse.ParseRequest$ParseRequestException: forbidden
        at com.parse.ParseRequest.newPermanentException(ParseRequest.java:391)
        at com.parse.ParseRESTCommand.onResponse(ParseRESTCommand.java:197)
        at com.parse.ParseRequest$3.then(ParseRequest.java:258)
        at com.parse.ParseRequest$3.then(ParseRequest.java:254)
        at bolts.Task$14.run(Task.java:796)
        at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
        at bolts.Task.completeAfterTask(Task.java:787)
        at bolts.Task.continueWithTask(Task.java:599)
        at bolts.Task.continueWithTask(Task.java:610)
        at bolts.Task$12.then(Task.java:702)
        at bolts.Task$12.then(Task.java:690)
        at bolts.Task$14.run(Task.java:796)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:841)

Thanks!

like image 418
Lilian Avatar asked Nov 01 '22 03:11

Lilian


2 Answers

I think you are trying to make a request with a user who doesn't have enough authorizathion to do it. Maybe, you are doing this request when the session is already destroyed(for logout),instead of doing it before destroying it.

like image 113
Andrea De Gaetano Avatar answered Nov 08 '22 03:11

Andrea De Gaetano


You have to delete it from Cloud Code using master key.

In the android client:

                //clear the installation backend
            HashMap<String, String> params = new HashMap<>();
            String idToken = ParseInstallation.getCurrentInstallation().getInstallationId();
            params.put("installationId", idToken);
            ParseCloud.callFunctionInBackground("removeInstallation", params, new FunctionCallback<String>() {
                @Override
                public void done(String response, ParseException e) {
                    if (e == null) {
                        //clear the local chache
                        ParseEasyAccess.clearParse();
                    } else {
                        e.printStackTrace();
                    }
                }
            });

And then in the Cloud Code:

         Parse.Cloud.define("removeInstallation", function(request, response) {

         Parse.Cloud.useMasterKey();

         var installationId = request.params.installationId;

         var query = new Parse.Query(Parse.Installation);
         query.equalTo("installationId", installationId);

         query.find(function(installations) {
             installations[0].destroy().then(
                 function() {
                     response.success("Destroyed");
                 },
                 function() {
                     response.error("Failed");
                 });
         });

     });

And if you wish to also delete the cached Installation on your device, do the following:

  1. Create a package named com.parse

  2. Crate a class, like for example MyParseTools.

  3. Make a static method liks so:

    public static void clearParseInstallation() {
    ParseInstallation.getCurrentInstallationController().clearFromDisk();
    ParseInstallation.getCurrentInstallationController().clearFromMemory();}
    
like image 33
Bolling Avatar answered Nov 08 '22 05:11

Bolling