This is my server side code or android side code. This code is working fine only for English messages. If I use Unicode charters like use Arabic language then it shows nothing in place of Arabic. In cause of English Arabic mix, it skip the only Arabic charters.
Kindly give me solution. Thanks!
This is my C# code
private string SendNotification(string authstring, string id, string msg)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true;
WebRequest request = WebRequest.Create("https://android.googleapis.com/gcm/send");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.Headers.Add(string.Format("Authorization: key={0}", authstring));
string collaspeKey = Guid.NewGuid().ToString("n");
string postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}", id, msg, collaspeKey);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
catch (Exception ex)
{
throw ex;
}
}
And this is my Android side code that catch the message.
@Override
protected void onMessage(Context context, Intent intent) {
String message = ArabicUtilities.reshape(intent.getExtras().getString("payload"));
}
Have you considered using base64 to encode the string sent through GCM? This way you would remove all encoding problems.
Here's what solved it for me in Java:
On the server side:
In the app:
André Oriani has the general idea for the fix. Even though the message is placed in the body of the request, it still needs to url encoded.
should be replaced with
You will need to add a reference to System.Web in order to use HttpUtility. See URL Encoding using C# for more information.