mwkirk / javapns

Test import of svn javapns repo from Google Code
3 stars 0 forks source link

JSONString to payload #96

Closed mwkirk closed 11 years ago

mwkirk commented 11 years ago

Original author: marcelba...@gmail.com (December 19, 2011 09:44:48)

hi, is there a way to get a JSONString into the Payload? if not maybe you could add

public void setPayloadFromJson(JSONObject payLoadJsonObject){ this.payload=payLoadJsonObject; }

thx

Original issue: http://code.google.com/p/javapns/issues/detail?id=96

mwkirk commented 11 years ago

From sype...@gmail.com on December 19, 2011 15:00:53 Could you explain how this would be useful? If you already have a JSONObject, it probably means that you were able to populate it yourself... so why not do it on the Payload's JSONObject (Payload.getPayload()) directly?

mwkirk commented 11 years ago

From marcelba...@gmail.com on December 19, 2011 15:26:04 because I need an Object of payload for sending a push notification and I can not cast the JSONObject to Payload class. Or is there an other way to send the notification? As I read I have to use an object of Payload class.

Please correct me if I can do this on an other way.

mwkirk commented 11 years ago

From sype...@gmail.com on December 19, 2011 15:39:39 Still, I do not understand what you are trying to do, sorry. Have you looked at the sample code posted in the wiki? Everything is there to allow you to send notifications.

If I'm totally missing the point, please provide some code that would use the "setPayloadFromJson" method you described, so I can better understand why you cannot use the library's current mechanism for creating and pushing payloads. Thank you.

mwkirk commented 11 years ago

From marcelba...@gmail.com on December 19, 2011 15:51:36 Ok I'll try again. I get an JSONString via my Webservice. I parse this JSONString to JSONObject and now I want to send these JSONObject as APNS-Payload.

BUT for sending I need an object of Payload class, not of JSONObject. Cast the JSONObject to Payload is not possible.

If I want to send the JSONObject i have, I have to read out all the information and pass it to the Payload object. BUT Payload-Object and JSONObject is the same so I don't really have to make these work-around.

As an example I have the simple JSONStrting "aps":{"alert":"test"}

I like to send this to my Device. I can't use /* Push your custom payload */ List notifications = Push.payload(payload, keystore, password, production, devices);

because payload has to be of Class Payload, and cast is not possible. so I have to work around, and read out the value of alert to pass the value to an payload object. Then I can send it. But this is totally long-winded, especially if I don't know the structure of the JSONStrings.

mwkirk commented 11 years ago

From sype...@gmail.com on December 19, 2011 16:07:41 Ok, what I think is confusing is that you are using "JSONString" and "JSONObject" class names (from the JavaPNS library) in your descriptions while I think you simply mean that you have a JSON-formatted string (that's not the same at all...).

So, you have a String that is already in JSON format and you'd like to push it. One way of doing it right now without modifications to the library is to subclass Payload and override the toString method:

    Payload payload = new Payload() {
        public String toString() {return yourJSONString;};
    };

You can then push your payload like any other payload.

mwkirk commented 11 years ago

From marcelba...@gmail.com on December 19, 2011 16:14:22

Ok, this is nearly the way I do it. I just thougth it would be helpful to have this In the lib.

Thx for your answer. (=

mwkirk commented 11 years ago

From sype...@gmail.com on December 19, 2011 18:12:27 It turns out that the JSONObject class already had a facility for parsing JSON strings. So, I wrote new constructors and methods using that facility and committed everything to the trunk (r345).

From now on, when you want to create a PushNotificationPayload from a JSON-formatted string, simply do this: PushNotificationPayload payload = PushNotificationPayload.fromJSON(yourJSONString);

Note that for your JSON string to be valid, it must be enclosed in {}. Your example in comment 5 would then have to be rewritten as: {"aps":{"alert":"test"}}.

Closing as fixed.