3

I have been reading this tutorial about Xamarin Android Push notifications using GCM, I have created the client APK and the asp.NET Server, but there's something I'm not able to use in ASP Project in here :

public string SendNotification(string deviceId, string message)....

var GoogleAppID = "123456789ABCDEFGHIJKLMNOP";
var SENDER_ID = "123456789";
var value = message;
var webRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
webRequest.Method = "post";
webRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
webRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
webRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
var postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
    + value + "&registration_id=" + deviceId + "";

I have the SENDER_ID and GoogleAppID, but what is the deviceId? The tutorial doesn't mention how to get this property, Does anyone know how to get this property! Thanks!

1 Answer 1

1

If you check the source of the tutorial here, it has a OnRegistered method in the client that creates the deviceId.

protected override void OnRegistered (Context context, string registrationId)
    {
        Console.WriteLine ("Device Id:" + registrationId);
        var preferences = GetSharedPreferences("AppData", FileCreationMode.Private);
        var deviceId = preferences.GetString("DeviceId","");
        if (string.IsNullOrEmpty (deviceId)) {
            var editor = preferences.Edit ();
            editor.PutString ("DeviceId", registrationId);
            editor.Commit ();
        }
    }

Also from the tutorial:

Once the device registration is successful, you will get the registration id in the OnRegistered event.

they have implemented the method like so:

protected override void OnRegistered (Context context, string registrationId)
{
    Console.WriteLine ("Device Id:" + registrationId);
}

Which is different from the source and might have caused confusion. I would have a look through the source to see if any other bits are missing from your implementation.

4
  • Sorry, but i'm not sure how to take the ID, but in my implementation i just have two classes : GcmService.cs and GcmBroadcastReceiver.cs, but this push notification will just work in my Phone ? i mean i can't use the same app in different phones and from my server send a message and everybody receive?
    – user3796130
    Aug 27, 2015 at 22:16
  • The tutorial your looking at sends one at a time, it concludes with "Now you can run the Android application, get the registration Id from output window. Run the ASP.Net application, provide the registration id and message. Click send notification. If everything goes well, you see a notification in the emulator." If you want to send a notification to all registered devices, you will have to store your registrationIds (aka deviceIds) and loop over them, from the documentation you can send the notification in batches of 1000 deviceIds. There is no send to "all devices" option in GCM.
    – Iain Smith
    Aug 27, 2015 at 22:49
  • Thank you for all! really help me!
    – user3796130
    Aug 27, 2015 at 22:56
  • For the "send to all devices" check the code in this link tejaprakash.com/2013/05/…
    – Iain Smith
    Aug 27, 2015 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.