Click here to Skip to main content
13,147,689 members (30,712 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have explained my question here..[^]

I am using PushSharp library to send push notification from my application.
PushService push = new PushService();
var reg_id_d = "APA91bETd-LsqnZjA-HKrnBOY3FbEhmWchpiwuhRkiv4gUdGDuvwDRB7YURICZ131XppDAUNUBLGe_vEPkQ-JR8UaVX7Y-NCkEfastCBLIYcUoFtt5cPafeKXHywi0WGDYW33ZQqr3oy";
var project_id_d = "482885626272";
var api_key_d = "AIzaSyAbh7R5KQR3KM7W_y-yS-Ao-JNiihNz7tE"; // "AIzaSyDcKfuW77GTwA46L6sqD41YhGf2j5S8o2w";
var package_name_d = "com.get.deviceid";
push.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings(project_id_d, api_key_d, package_name_d));
                .WithJson("{\"alert\":\"Alert Text!\",\"badge\":\"1\"}"));

I am getting notification on my device but with blank message..

I have tried with sever code available in C# to send GCM push notification, but getting same problem of having blank message.

I tried using PHP to send notification. and it is working as expected. so, I am not sure what is wrong in my above code. Can anyone please help me on this?
Posted 7-Nov-12 19:53pm
MinhajAli 22-Jun-16 14:58pm
I am getting the same problem. could you please help.
how do you solve the issue.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In this article I will try to explain how we can integrate a push notification service for Android using ASP.NET and C#. We all know that mobile applications are booming the market trend. Some custom mobile applications use the push notification service to give updates to application users. Here I will explain how we can use Google’s GCM push notification service.

Class file "AndroidGCMPushNotification.cs"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;
/// <summary>
/// Summary description for AndroidGCMPushNotification
/// </summary>
public class AndroidGCMPushNotification
	public AndroidGCMPushNotification()
		// TODO: Add constructor logic here
    public string SendNotification(string deviceId, string message)
        string GoogleAppID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";        
        var SENDER_ID = "9999999999";
        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("");
        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
        // string postData = "{ 'registration_id': [ '" + regId + "' ], 'data': {'message': '" + txtMsg.Text + "'}}";
        string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;
        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        WebResponse tResponse = tRequest.GetResponse();
        dataStream = tResponse.GetResponseStream();
        StreamReader tReader = new StreamReader(dataStream);
        String sResponseFromServer = tReader.ReadToEnd();
        return sResponseFromServer;

You can call SendNotification function by passing device Id and Message.
AndroidGCMPushNotification apnGCM = new AndroidGCMPushNotification();
string strResponse = apnGCM.SendNotification("9999xxxxxxxxxxxxxxxx", "Test Push Notification message ");
SoMad 13-Feb-13 5:24am
Looks like you got that from here: Android push notification implementation using ASP.NET and C#[^].

Remember to reference your source.

Soren Madsen
Member 10660142 2-Apr-14 5:37am
i implement this code but below error is generated
The remote server returned an error: (401) Unauthorized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
Mukund Thakker 5-Apr-14 3:12am
Please confirm your GoogleAppID and SENDER_ID
string GoogleAppID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var SENDER_ID = "9999999999";
Somasundharam Arjunan 26-Jun-14 1:35am
am trying lot but again getting "Unauthorized (401) Error" in WebResponse tResponse = tRequest.GetResponse(); and am checked the api key and sender id both are same.
MinhajAli 22-Jun-16 15:00pm
using the above code I am getting notification but it's blank (without message) .
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

check 'alert' keyword exact same in android app (in coding) also...when this keyword matches then your message send via push notification...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100