So i’m trying to send some data from my c# server to my gcm android application. It works just fine(after whole day of work) but i can’t figure out how to actualy send some data(and recieve it). On my c# side i have:
using (var wc = new WebClient())
{
wc.Headers.Add("Authorization", "key=" + "key");
var nameValues = new NameValueCollection
{
{"registration_id", "id"},
{"collapse_key", Guid.NewGuid().ToString()},
{"data.payload", "works!!!!!!"},
{"message", "YES it works!!!"}
};
var resp = wc.UploadValues("https://android.googleapis.com/gcm/send",nameValues);
var respMessage = Encoding.Default.GetString(resp);
MessageBox.Show("Got respose from GCM: " + respMessage);
}
and in my onmessage method on android application i have:
protected void onMessage(Context arg0, Intent arg1) {
Log.i("Registration", "Got a message!");
Log.i("Registration", arg0.toString() + " " + arg1.toString());
String score = arg1.getExtras().getString("message");
score=arg1.getExtras().getString("data.payload");
The problem is that my score String is always null :S
Any ideas?
The rule is that POST fields called “data.Foo” are passed as intent extras called “Foo”. To retrieve an extra called “payload”, you need to pass “data.payload” and retrieve `arg1.getStringExtra(“payload”).
By the way, you’re retrieving score twice – as “message” and as “data.payload”. Neither is right.