Parse.com filtering Push Notification as "multi server"

44 Views Asked by At

first I apologize for my poor English. I'm just approaching to Parse.com and I'm developing a "multi-server" application that allows me to send notifications filtered by "server".

I tried two solutions but both have some issue all connected to the fact that when i start my Application i don't know which "server" the user will choose.

Idea 1 - Real Multi Server

  1. I create manually n Parse.com servers, totally separated each other
  2. I store all my servers keys in objects like [Name, AppID, clientKey]
  3. My application lists the name attribute
  4. The user choose his server
  5. I start new Activity in which onCreate() I initialize Parse like:

    String appID = getAppID();
    String cKey = getKey();
    Parse.initialize(this, appID, cKey);
    ParseInstallation.getCurrentInstallation().saveInBackground();
    
  6. (If he want to change server i simply clean my App data and re-initialize Parse with the new keys)

  7. Everything works fine until I try to send a Push Notification when the the app is closed (not active and not in background) when I have a NullPointerException due to initialization of Parse not yet called.

Idea 2 - Simulated Multi Server

  1. I create manually ONE Parse.com server
  2. All tables have a new Column like (just as example) uniqueIdServer
  3. I initialize Parse in Application like:

    public class MyApplication extends Application {
    @Override
    public void onCreate() {
        Parse.initialize(this, "ThisTimeIHaveJustOneAppIDIt'sEasy", "SameHere");
        ParseInstallation.getCurrentInstallation().saveInBackground();
        super.onCreate();
    }}
    
  4. I display my old list but this time is just [name, uniqueID]

  5. The user choose his "server"
  6. Now i can easy filter my data with my new column and i have no problem when my app is closed because Parse will call his initializer by his own
  7. But i don't know how i can filter Push Notification just for some uniqueId
  8. I tried using channels and than sending Push only at that channel:

        List<String> channels = new ArrayList<>();
    channels.add(getUniqueID());
    ParseInstallation install = ParseInstallation.getCurrentInstallation();
    install.put("channels", channels);
    install.saveEventually();
    
  9. But is always the same, i dont know at "Application time" what my user will choose so my getUniqueID() will bind "null" or similar and even worse i don't know how can i change channel (atm i can only if i uninstall my app)

Really thanks at all, any help will be really appreciated.

1

There are 1 best solutions below

0
On

Using your idea 2, installations can be given custom attributes and queried exactly as any other objects. Moreover, pushes can be made with an installation query. See "advanced targeting" in the docs.

Minimally, you can say something like this:

var query = new Parse.Query(Parse.Installation);
query.equalTo('uniqueIdServer', 'serverId123');

var data = { alert: "Ciao users of serverId123!" }

Parse.Push.send({ where: query, data: data }).then(function() {
    // this code runs after the push has been sent to APN
}, function(error) {
    // this code runs in case of an error
});