[英]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 : 我一直在阅读关于使用GCM的Xamarin Android推送通知的本教程,我已经创建了
client APK
和asp.NET
服务器,但是我在ASP项目中无法使用这里:
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 + "®istration_id=" + deviceId + "";
I have the SENDER_ID
and GoogleAppID
, but what is the deviceId
? 我有
SENDER_ID
和GoogleAppID
,但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. 如果您在此处检查教程的来源,则它在客户端中具有创建deviceId的OnRegistered方法。
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.
设备注册成功后,您将在OnRegistered事件中获得注册ID。
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.
我将通过源代码查看您的实现中是否缺少任何其他位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.