Android 如何使用Parse避免重复安装?
我注意到,每次我将应用程序部署到手机上时,它都会复制parse中的安装以接收推送通知。每当我重新安装应用程序时,如何避免这种情况发生?经过一周的研究和反复尝试,我终于找到了一个可行的解决方案。基本上,您需要做两件事来实现这一点:Android 如何使用Parse避免重复安装?,android,parse-platform,installation,Android,Parse Platform,Installation,我注意到,每次我将应用程序部署到手机上时,它都会复制parse中的安装以接收推送通知。每当我重新安装应用程序时,如何避免这种情况发生?经过一周的研究和反复尝试,我终于找到了一个可行的解决方案。基本上,您需要做两件事来实现这一点: 在您的Android应用程序中:在初始化并保存ParseInstallation时传递一些唯一的ID。我们将使用 在解析云代码中:在保存新的安装之前,请检查其唯一ID是否存在于旧的安装中。如果是,请删除旧的 如何做: 在应用程序的onCreate()方法中: /
- 在您的Android应用程序中:在初始化并保存
时传递一些唯一的ID。我们将使用ParseInstallation
- 在解析云代码中:在保存新的
安装之前,请检查其唯一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."); }); });
Parse.Cloud.beforeSave(Parse.Installation,函数(请求,响应){ Parse.Cloud.useMasterKey(); var androidId=request.object.get(“androidId”); if(android==null | | android==“”){ console.warn(“未找到Android,保存并退出”); 回答:success(); } var query=newparse.query(Parse.Installation); query.equalTo(“android”,android); query.addAscending(“createdAt”); query.find().then(函数(结果){ 对于(变量i=0;i
您可能想知道的事情:
- Android设备没有完美的唯一标识符。在我的情况下,我使用了。你可以用别的东西。为了更好地了解情况
- 注意,在调用
第一次,一个名为put(“android”,android)之后
的新列将添加到Parse-App仪表板中的android
表视图中安装
参考资料:[]、[]、[]我们需要更多信息。我的名字是什么?你在用Gradle吗?如果是这样,请发布相关的build.gradle文件。我正在使用Eclipse IDE,还需要更多信息。。。“复制安装”是什么意思?重复申请?那么另一个
packageName
?我编辑了我的文章,通过安装我指的是解析API。不是应用程序安装本身。可能是因为每次重新安装都有一个不同的hashkey,Parse use知道安装和处置是相同的,当这个条件不相等时,Parse API会使新安装不再重复。