简体   繁体   中英

Android Parse push notifications showing after reinstall

I'm using the Parse push notifications in my project. The notifications work fine throughout the app.

The client has to subscribe to channels by manually adding them. The channels are saved in a shared preferences array. We don't work with user accounts.

The problem: Whenever I reinstall the application (Thus clearing the shared preferences / channels) , I still receive push notifications once or twice before the Parse server gets in sync with the channel list (empty).

I basically receive 1-2 or 3 push notifications from Parse on an empty channels list...

Something I found out with testing is the following: I used a first installation with the right client+app key. I added some channels and uninstalled the app after. Before reinstalling the app again I created made up keys in the code and reinstalled the app under bad keys.... But I could still receive notifications under the wrong key (throughout the entire app) .. even though I received an unauthorized exception. The parse push log on the website states it didn't sent the push to any device, which is absolutely false.

In my application class I make sure to unsubscribe to my channels just to be safe;

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();

    }
}

I can disable notifications within the app. If I do that, and THEN reinstall the app, I wont get false pushes anymore. But I can't put that code in an onDestroy / onPause and this wouldn't be the "clean" way.

I fixed the problem by adding some JS Cloud code on Parse and adding a custom installation ID in the Android app. This prevents new installation objects to receive old notifications.

It seems that this is a problem which occurs on Android only. I added the following code ( found online ) in my application to make it work:

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

Deploying code to the Cloud can be difficult, follow this tutorial on how to ( Select your OS on the site ): https://parse.com/apps/quickstart#cloud_code/

Carefully read the guide because it's easy to mess up, but this is all I did to deploy code to the cloud.

After you've added the cloud code, login to parse and select your application. Then in the menu select cloud, and it should show your deployed JS (main.js) from GitHub.

After that for testing purposes you can select "Data" under the core to keep watch over the installation object, to make sure the installation object is fixed.

I hoped this helped :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM