Question

Actually I'm working with PushSharp. I would like to send notification but I have to put variable into this JSON instead of hardcoded text (below example).

var msg = "TEST MESSAGE";
    push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(registrationId)
                             .WithJson("{\"alert\":\"HERE MESSAGE\",\"badge\":7,\"sound\":\"sound.caf\"}"));

Is it possible? How can I put msg variable into

I tried something like this:

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(registrationId)
                         .WithJson("{\"alert\":\"{0}\",\"badge\":7,\"sound\":\"sound.caf\"}",msg));

but it says: No overload for method 'WithJson' takes '3' arguments.

Any idea's how to solve this problem?

Was it helpful?

Solution

If you want to add your message into JSON you could do this:

var msg = "my message"; // this would be set somewhere else in the code
var jsonObject = {
    "alert" : "{0}",
    "badge" : "7",
    "sound" : "sound.caf",
    "msg" : msg
};

// convert the object into a string
var jsonString = JSON.stringify(jsonObject);

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(registrationId)
        .WithJson(jsonString));

Edit: I changed the code so you don't have to manipulate the string. Instead you can now manipulate the jsonObject. JSON.stringify converts it into a string then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top