【问题标题】:Android Parse push notifications showing after reinstall重新安装后显示 Android Parse 推送通知
【发布时间】:2015-10-19 12:53:24
【问题描述】:

我在我的项目中使用 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 中,这不是“干净”的方式。

【问题讨论】:

    标签: android parse-platform notifications


    【解决方案1】:

    我通过在 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 "";
    }
    

    ma​​in.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/

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

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

    之后出于测试目的,您可以选择核心下的“数据”来监视安装对象,以确保安装对象是固定的。

    我希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2016-10-16
      相关资源
      最近更新 更多