hengsokchamroeun / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

JSONString to payload #96

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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 reported on code.google.com by marcelba...@gmail.com on 19 Dec 2011 at 9:44

GoogleCodeExporter commented 8 years ago
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?

Original comment by sype...@gmail.com on 19 Dec 2011 at 3:00

GoogleCodeExporter commented 8 years ago

Original comment by sype...@gmail.com on 19 Dec 2011 at 3:04

GoogleCodeExporter commented 8 years ago
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.

Original comment by marcelba...@gmail.com on 19 Dec 2011 at 3:26

GoogleCodeExporter commented 8 years ago
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.

Original comment by sype...@gmail.com on 19 Dec 2011 at 3:39

GoogleCodeExporter commented 8 years ago
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<PushedNotification> 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.

Original comment by marcelba...@gmail.com on 19 Dec 2011 at 3:51

GoogleCodeExporter commented 8 years ago
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.

Original comment by sype...@gmail.com on 19 Dec 2011 at 4:07

GoogleCodeExporter commented 8 years ago

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.
(=

Original comment by marcelba...@gmail.com on 19 Dec 2011 at 4:14

GoogleCodeExporter commented 8 years ago
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.

Original comment by sype...@gmail.com on 19 Dec 2011 at 6:12