如何使用 Parse 避免安装重复? [英] How to avoid Installation duplicates using Parse?
本文介绍了如何使用 Parse 避免安装重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我注意到每次我将我的应用程序部署到我的手机时,它都会在解析中复制安装以接收推送通知.每当我重新安装应用程序时,如何避免这种情况发生?
I've noticed that every time I've deployed my app to my phone,it duplicates the installation in parse to receive push notifications. How can I avoid this from happening whenever I reinstall the app?
推荐答案
经过一周的研究和尝试,我终于找到了一个可行的解决方案.基本上,您需要做两件事来实现这一点:
After a week of research and try and error, I finally found a working solution. Basically, you need to do two things to achieve this:
- 在您的 Android 应用中:在您初始化并保存
ParseInstallation
时传递一些唯一 ID.我们将使用 ANDROID_ID. - 在解析云代码中:在保存新的
安装
之前,检查它的唯一 ID 是否存在于旧的安装
中.如果是,请删除旧的.
- In your Android app: pass some unique ID when you initialize and save the
ParseInstallation
. we will use ANDROID_ID. - In Parse Cloud Code: Before you save a new
Installation
, check to see if its unique ID exist in an oldInstallation
. If it does, delete the old one.
怎么做:
在您的应用程序的
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();
在您的云代码中,添加:
In your cloud code, add this:
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 Dashboard 中的表格视图.
- There is no perfect unique identifier for Android devices. In my case, I used ANDROID_ID. You might use something else. Read this official article to get a better picture.
- Note that after calling
put("androidId", android_id);
for the first time, a new column namedandroidId
will be added to yourInstallation
table view in the Parse App Dashboard.
Resources: [1], [2], [3]
这篇关于如何使用 Parse 避免安装重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文