Skip to content

Instantly share code, notes, and snippets.

@mbmc
Last active November 25, 2019 14:36
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mbmc/ebe5b65bc4eb86904c78 to your computer and use it in GitHub Desktop.
Save mbmc/ebe5b65bc4eb86904c78 to your computer and use it in GitHub Desktop.
Delete Parse duplicated Installation object for Android app
Context:
When you install an app on Android, it creates an entry (Installation) in the database.
If you uninstall the app, then re-install it, another entry is created. If you subscribe to a channel, you'll get 2 push notifications.
On iOS, this problem doesn't exist, and with UrbanAirship, there's no problem either.
Solution:
The idea is to remove all the entries that matches a "unique Id" created for each device.
Gist based on [this post](https://www.parse.com/questions/check-for-duplicate-installations-of-same-user-on-re-installation-of-app)
I chose to use the Wifi mac address as unique key, because based on [that post](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id),
there's no good way to get a unique Id.
// Android app
private void setupParse(Context context) {
Parse.initialize(this, <PARSE_APP_ID>, <PARSE_CLIENT_KEY>);
ParseInstallation.getCurrentInstallation().put("uniqueId", getWifiMacAddress(context));
ParseInstallation.getCurrentInstallation().saveInBackground();
}
private String getWifiMacAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null && wifiManager.getConnectionInfo() != null) {
return wifiManager.getConnectionInfo().getMacAddress();
}
return "";
}
// Parse CloudCode
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
var uniqueId = request.object.get("uniqueId");
if (uniqueId == null || uniqueId == "") {
console.warn("No uniqueId found, exit");
response.success();
}
var query = new Parse.Query(Parse.Installation);
query.equalTo("uniqueId", uniqueId);
query.addAscending("createdAt");
query.find().then(function(results) {
for (var i = 0; i < results.length; ++i) {
if (results[i].get("installationId") != request.object.get("installationId")) {
console.warn("App id " + results[i].get("installationId") + ", delete!");
results[i].destroy().then(function() {
console.warn("Delete success");
},
function() {
console.warn("Delete error");
}
);
} else {
console.warn("Current App id " + results[i].get("installationId") + ", dont delete");
}
}
response.success();
},
function(error) {
response.error("Can't find Installation objects");
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment