简体   繁体   English

如何在Xamarin Android中使用Google Cloud Messaging(GCM)

[英]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 APKasp.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 + "&registration_id=" + deviceId + "";

I have the SENDER_ID and GoogleAppID , but what is the deviceId ? 我有SENDER_IDGoogleAppID ,但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.

相关问题 Xamarin-Google Cloud Messaging-错误:包com.google.android.gms.iid不存在 - Xamarin - Google Cloud Messaging - error: package com.google.android.gms.iid does not exist Xamarin Google Cloud Messaging GcmClient.CheckDevice(this)导致运行时错误 - Xamarin Google Cloud Messaging GcmClient.CheckDevice(this) causes runtime error XAMARIN - Firebase Cloud Messaging - Android 设备不显示通知但他收到通知 - XAMARIN - Firebase Cloud Messaging - Android Device not display notification but he receives it 将Google Cloud Messaging(GCM)推送通知接收到UWP Windows 10应用程序 - Receive Google Cloud Messaging (GCM) Push Notification to a UWP Windows 10 App 可以使用.NET桌面应用程序作为Google Cloud Messaging通知的接收者吗? - Is it possible to use a .NET desktop application as the receiver for Google Cloud Messaging notifications? 如何使用Xamarin Forms和Google Direction API android - How to use Xamarin Forms and Google Direction API android 在Xamarin.Android本机级别的Firebase消息服务中使用Xamarin.Forms.Application.Current.Properties - Use Xamarin.Forms.Application.Current.Properties in Firebase Messaging Service on Xamarin.Android Native Level 如何在 Xamarin.iOS 中使用 FCM(Firebase 云消息传递)发送 iOS 推送通知 - How to send iOS push notifications using FCM(Firebase cloud messaging) in Xamarin.iOS Xamarin Android GCM关闭VM - Xamarin Android GCM Shutting Down VM 如何使用setPaintFlags-Xamarin,Android - How to use setPaintFlags - xamarin, android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM