1

I have sent push notification using ASP.net C# by GCM to android mobiles.

But I tried different kinds of code but all arr return Missing Registration error, So please help me.

Mat
  • 202,337
  • 40
  • 393
  • 406

2 Answers2

0

try PushSharp. it's pretty straightforward. and check this instruction - How to Configure & Send GCM Google Cloud Messaging Push Notifications using PushSharp

var push = new PushBroker();

//Registering the GCM Service and sending an Android Notification
push.RegisterGcmService(new GcmPushChannelSettings("theauthorizationtokenhere"));
//Fluent construction of an Android GCM Notification
//IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself!
push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE")
                      .WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"));

You can install it via nuget.

Dmitry Khryukin
  • 6,408
  • 7
  • 36
  • 58
  • i am using this code. http://www.codeproject.com/Tips/434338/Android-GCM-Push-Notification. Can you suggest like this – Jegannathan k May 06 '13 at 06:44
  • @user2353494 does your phone support GCM? do you have a google account on this phone? – Dmitry Khryukin May 06 '13 at 07:16
  • ya Dmitry i have all. or else can u just how to configure pushsharp to my asp.net web application. i struggled in that point because i am using framework 2.0. – Jegannathan k May 06 '13 at 07:29
  • @user2353494 I hope you mean PushSharp 2.0, not .NET 2.0. just install the package from nuget - http://nuget.org/packages/PushSharp/ and than follow the instructions from there - https://github.com/Redth/PushSharp (Sample code). I'll update the answer. – Dmitry Khryukin May 06 '13 at 07:47
  • hi dimi i tried but it still some exception thrown.But can you go throw the above code from codeproject http://www.codeproject.com/Tips/434338/Android-GCM-Push-Notification tell why it is happen please buddy .... :) – Jegannathan k May 06 '13 at 11:23
  • @user2353494 have you tried PushSharp? which exception do you see? what does it say? – Dmitry Khryukin May 06 '13 at 21:02
  • @user2353494 can you post the error? does it appear using PushSharp? – Dmitry Khryukin May 07 '13 at 05:28
  • Cannot implicitly convert type 'string' to 'Newtonsoft.Json.Linq.JToken' here it is buddy... – Jegannathan k May 07 '13 at 05:35
  • can you tell the step by step configuration for pushsharp to send push to android mobiles – Jegannathan k May 07 '13 at 05:44
  • @user2353494 it's here - https://github.com/Redth/PushSharp/wiki/How-to-Configure-&-Send-GCM-Google-Cloud-Messaging-Push-Notifications-using-PushSharp just set up the package to your project - http://nuget.org/packages/PushSharp – Dmitry Khryukin May 07 '13 at 05:46
  • hi buddy can u suggest another way for push notification uasing C# except pushsharp – Jegannathan k May 07 '13 at 05:51
  • @Jegannathank no, I used only this library. it's very easy to use it. – Dmitry Khryukin May 07 '13 at 06:08
  • buddy i finally got push notification . here is code http://stackoverflow.com/questions/11261718/gcm-push-notification-with-asp-net/11651066#11651066 thanks for timely reply – Jegannathan k May 07 '13 at 06:59
  • @Jegannathank it's good. no worries, welcome. try to use PushSharp next time - it's really cool library. – Dmitry Khryukin May 07 '13 at 07:08
0
 public class GCMSNS
{
    public static string SendGCMNotification(string deviceId, string message)
    {
        string GoogleAppID = "XYxxxxxxxxxxxxxxxxxxxx";//This is API Key Server
        var SENDER_ID = "11111111111111"; //This is Google Project Id
        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 = "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;
    }
}
Sanjay Dwivedi
  • 699
  • 7
  • 10