GCM Decode

iCAB

Well-Known Member
Licensed User
Longtime User
Hi There

Can someone tell me what is the best way to decode this type of message received from GCM server ( In particular I need the contents of the data block )

[ code ]
{
"registration_ids": [ "SomeKey" ],
"data": {"tickerText":"hello", "contentTitle":"hello2", "message": "Test Push Notification message "},
"collapse_key": "test",
}
[ /code ]


I am using the following code that I extracted from one of the samples

[ code ]
Sub MessageArrived (Intent As Intent)

Dim From, CollapseKey, data As String, data2 As String, data3 As String, datamessage As String

If Intent.HasExtra("from") Then
From = Intent.GetExtra("from")
End If

If Intent.HasExtra("data") Then
data = Intent.GetExtra("data")
End If


If Intent.HasExtra("collapse_key") Then
CollapseKey = Intent.GetExtra("collapse_key")
End If

'Here you should handle the new message:
Log("New message arrived: " & data & " " & data2 & " " & data3)

ToastMessageShow("New message: " & data, True)

End Sub

[ /code ]

This section of the code never returns true
[ code ] Intent.HasExtra("data") [ code ]


Thanks
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel
Thanks for the reply. Sorry, but there 2 things that I don't understand clearly:

1. in the above example, in MessageArrived I added the following code :
B4X:
strTemp = Intent.ExtrasToString
Log( strTemp )
The log window showed:Bundle[{message=Test Push Notification message , from=173276336318, collapse_key=test, tickerText=hello, contentTitle=hello2}]

Which is actually different than what I have constructed.
Any ideas?

2. You are saying that I should use the JSON library to parse the string. Ok no problem, but can you please provide me the code that shows how to extract the JSON object from the intent.

Thanks in advance
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
This is the code I am using
B4X:
public string SendNotification(string deviceId, string message)
        {
            string GoogleAppID = "AppIDInhere";
            var SENDER_ID = "SenderIDInhere";
            var value = message;
            string postData;

            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";
            //tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";

            tRequest.ContentType = " application/json";

            tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

            postData = "{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
                           "\"data\": {\"parameters\":\"" + "15239" + "\", " +
                           "\"timesent\":\"" + System.DateTime.Now.ToString() + "\", " +
                           "\"button1\":\"" + "Confirm" + "\", " +
                           "\"button2\":\"" + "" + "\", " +
                           "\"button3\":\"" + "" + "\", " +
                           "\"message\": \"" + message + "\"}," + 
                           "\"collapse_key\": \"" + "REFRESH_SCHEDULE" + "\", " +
                            "}"; 

            Console.WriteLine("\n");
            Console.WriteLine(postData);

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            Console.WriteLine(sResponseFromServer);

            tReader.Close();
            dataStream.Close();
            tResponse.Close();
            return sResponseFromServer;
        }
    }
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…