其他分享
首页 > 其他分享> > android-如何避免使用Parse进行安装重复?

android-如何避免使用Parse进行安装重复?

作者:互联网

我注意到,每次将应用程序部署到手机上时,它都会重复解析安装以接收推送通知.重新安装应用程序时如何避免这种情况发生?

解决方法:

经过一周的研究和反复尝试,终于找到了可行的解决方案.基本上,您需要做两件事来实现此目的:

>在您的Android应用中:初始化并保存ParseInstallation时传递一些唯一的ID.我们将使用ANDROID_ID.
>在“解析云代码”中:在保存新安装之前,请检查其唯一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.");
    });
});

就是这样!

您可能想知道的事情:

> Android设备没有完美的唯一标识符.就我而言,我使用ANDROID_ID.您可能会使用其他名称. Read this official article以获得更好的画面.
>请注意,在调用put(“ androidId”,android_id);之后,首次将名为androidId的新列添加到Parse App仪表板的“安装”表视图中.

资源:[1],[2],[3]

标签:parse-platform,installation,android
来源: https://codeday.me/bug/20191120/2046931.html