重新安装

时间:2015-10-19 12:53:24

标签: android parse-platform notifications

我在我的项目中使用Parse推送通知。整个应用程序中的通知工作正常。

客户必须通过手动添加来订阅频道。通道保存在共享首选项数组中。我们不使用用户帐户。

问题:每当我重新安装应用程序时(因此清除共享首选项/频道),在Parse服务器与频道列表同步(空)之前,我仍然会收到一次或两次推送通知。

我基本上会在Parse的空频道列表上收到1-2或3个推送通知......

我通过测试发现的东西如下:我使用正确的客户端+应用程序密钥进行了第一次安装。我添加了一些频道并在之后卸载了应用程序。在重新安装应用程序之前,我在代码中创建了组成密钥并在坏密钥下重新安装了应用程序....但我仍然可以在错误的密钥(整个应用程序)下收到通知..即使我收到了未经授权的例外。网站上的解析推送日志表明它没有将推送发送到任何设备,这绝对是错误的。

在我的应用课程中,我确保取消订阅我的频道只是为了安全;

public class Application extends android.app.Application {

@Override
public void onCreate() {

    Parse.initialize(this, "APP_ID", "CLIENT_ID");
    ParseInstallation.getCurrentInstallation().saveInBackground();

    ParseInstallation install = ParseInstallation.getCurrentInstallation();
    install.remove("channels");
    install.saveInBackground();

    super.onCreate();

    }
}

我可以在应用内禁用通知。如果我这样做,然后重新安装应用程序,我就不会再进行虚假推送了。但是我无法将该代码放在onDestroy / onPause中,而且这不会是" clean"方式。

1 个答案:

答案 0 :(得分:0)

我通过在Parse上添加一些JS Cloud代码并在Android应用中添加自定义安装ID来解决问题。这可以防止新安装对象接收旧通知。

似乎这是仅在Android上出现的问题。我在我的应用程序中添加了以下代码(found online)以使其工作:

<强> Application.java

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 "";
}

<强> main.js

// 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");
    }
  );
});

将代码部署到云可能很困难,请按照本教程了解如何(在网站上选择您的操作系统):https://parse.com/apps/quickstart#cloud_code/

仔细阅读本指南,因为它很容易搞砸,但这就是我将代码部署到云端所做的全部。

在您添加了云代码后,登录解析并选择您的应用程序。然后在菜单中选择cloud,它应该显示你从GitHub部署的JS(main.js)。

之后出于测试目的,您可以选择&#34;数据&#34;在核心下保持监视安装对象,以确保安装对象是固定的。

我希望这有助于:)