Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Android Push
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.
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 7-Nov-12 20:53pm
Rate this: bad
good
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("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 ");
  Permalink  
v2
Comments
SoMad at 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 at 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 at 5-Apr-14 3:12am
   
Please confirm your GoogleAppID and SENDER_ID
string GoogleAppID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var SENDER_ID = "9999999999";
Somasundharam Arjunan at 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.
Rate this: bad
good
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...
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2014
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