我注意到每次我将我的应用部署到手机上时,它都会在解析时复制安装以接收推送通知。每当我重新安装应用程序时,如何避免这种情况发生?
答案 0 :(得分:3)
经过一周的研究和尝试和错误,我终于找到了一个有效的解决方案。基本上,你需要做两件事来实现这个目标:
ParseInstallation
时传递一些唯一ID。我们将使用ANDROID_ID。Installation
之前,请检查旧Installation
中是否存在其唯一ID。如果是,请删除旧的。如何操作:
在您的应用的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.");
});
});
那就是它!
您可能想知道的事情:
put("androidId", android_id);
后,名为androidId
的新列将添加到Parse App Dashboard中的Installation
表视图中。