简体   繁体   中英

How to use Google Cloud Messaging (GCM) in Xamarin Android

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM