Click here to Skip to main content
15,885,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have explained my question here..
http://stackoverflow.com/questions/13268460/pushsharpandroid-gcm-push-notification-received-without-push-message[^]

I am using PushSharp library to send push notification from my application.
C#
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));
push.QueueNotification(NotificationFactory.AndroidGcm()
                .ForDeviceRegistrationId(reg_id_d)
                .WithCollapseKey("NONE")
                .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
Comments
MinhajAli 22-Jun-16 14:58pm    
I am getting the same problem. could you please help.
how do you solve the issue.

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("https://android.googleapis.com/gcm/send");
        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 + "";
        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();

        
        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        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 ");
 
Share this answer
 
v2
Comments
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) .
check 'alert' keyword exact same in android app (in coding) also...when this keyword matches then your message send via push notification...
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900