Xamarin Google Cloud Messaging GcmClient.CheckDevice(this) 导致运行时错误

Xamarin Google Cloud Messaging GcmClient.CheckDevice(this) causes runtime error

所以我尝试使用 Xamarin 中的 Google 云消息客户端组件向 GCM 注册我的应用程序。

当我在我的应用程序中 运行 此代码时,它会在 GcmClient.CheckDevice(this) 方法调用时出现 运行time 错误。该按钮位于主 activity 中,这样我就可以控制何时开始注册过程。

我正在使用 Xamarin 播放器模拟器加载应用程序。

button.Click += (object sender, EventArgs e) => 
        {
            GcmClient.CheckDevice(this);
            GcmClient.CheckManifest(this);

            var preferences = GetSharedPreferences("AppData", FileCreationMode.Private);
            var deviceid = preferences.GetString("DeviceId", "");
            if (string.IsNullOrEmpty(deviceid))
            {
                Toast.MakeText(this, "I'm here", ToastLength.Short).Show();
                GcmClient.Register(this, GcmBroadcastReceiver.SENDER_IDS);
            }

        };

如有任何帮助,我们将不胜感激。 这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="my_package_name">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
    <application android:label="patientFlowNotifications"></application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
</manifest>

默认情况下,Xamarin 播放器模拟器不附带 Google Play 服务。您需要手动安装它们才能使用 GCM:

http://developer.xamarin.com/guides/android/getting_started/installation/android-player/

The Google Apps and Google Play Services have been packaged by independent developers in the Android community into a flashable zip format. It is possible to install Google Apps and Google Play Services on the Xamarin Android Player yourself by following these steps:

Download the package from the internet. There are many sources for this, one possible source is the CyanogenMod web site. Start up the Android Player and unlock it. Drag and drop the zip file that you downloaded onto the Android Player. Restart the Android Player.

另一点不会不可避免地导致异常但会阻止您的应用程序 运行 如预期的那样是您没有在清单中声明任何意图过滤器并且缺少特定于应用程序的 CDM-Permission。

<permission android:name="your.package.name.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />