简体   繁体   中英

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

So I am trying to register my application with GCM using the Google Cloud Messaging Client component in Xamarin.

when ever I run this code in my application, however, it gets a runtime error at the GcmClient.CheckDevice(this) method call. The button is in the main activity and its just so I have control of when I start the registration process.

I am using the Xamarin player emulator to load the app.

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);
            }

        };

Any aid would be greatly appriciated. Here is my manifest

<?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>

The Xamarin Player Emulator doesn't ships with Google Play Services by default. You need to manually install them in order to use 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.

Another point that will not inevitably cause an exception but will prevent your app from running as expected is that you have not declared any intent filters in your manifest and a app-specific CDM-Permission is missing.

<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" />

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