我的编码中出现GCM SERVICE_NOT_AVAILABLE错误

时间:2014-04-11 09:42:09

标签: android google-cloud-messaging service-not-available

您好我为我的项目创建了gcm,但我无法获得注册ID,这是我的编码

对于GCM请求

public void gcmrequest() {
    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");

    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));

    registrationIntent.putExtra("sender","1241241766");

    this.startService(registrationIntent);
    Log.i("GCM Request", "GCM Request");
}

这是我的BroadcastReceiver代码

public class GcmReceiver extends BroadcastReceiver {

private static String KEY = "c2dmPref";
private static String REGISTRATION_KEY = "registrationKey";

private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    Log.i("onReceive", "onReceive");
    if (intent.getAction().equals(
            "com.google.android.c2dm.intent.REGISTRATION")) {
        Log.i("REGISTRATION", "REGISTRATION");
        handleRegistration(context, intent);
    } else if (intent.getAction().equals(
            "com.google.android.c2dm.intent.RECEIVE")) {
        Log.i("RECEIVE", "RECEIVE");
        handleMessage(context, intent);
    }
}

private void handleMessage(Context context2, Intent intent) {
    String a = intent.getAction();
    if (a.equals("com.google.android.c2dm.intent.RECEIVE")) {

    }
}

private void handleRegistration(Context context, Intent intent) {
    String registration = intent.getStringExtra("registration_id");
    if (intent.getStringExtra("error") != null) {

        Log.d("c2dm", "registration failed");

        String error = intent.getStringExtra("error");
        Log.d("c2dm", error);
        if (error == "SERVICE_NOT_AVAILABLE") {
            Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
        } else if (error == "ACCOUNT_MISSING") {
            Log.d("c2dm", "ACCOUNT_MISSING");
        } else if (error == "AUTHENTICATION_FAILED") {
            Log.d("c2dm", "AUTHENTICATION_FAILED");
        } else if (error == "TOO_MANY_REGISTRATIONS") {
            Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
        } else if (error == "INVALID_SENDER") {
            Log.d("c2dm", "INVALID_SENDER");
        } else if (error == "PHONE_REGISTRATION_ERROR") {
            Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
        }
    } else if (intent.getStringExtra("unregistered") != null) {

        Log.d("c2dm", "unregistered");
        Toast.makeText(context, "C2DM unregistered sucessfully",
                Toast.LENGTH_SHORT).show();

    } else if (registration != null) {
        Log.d("c2dm", registration);
        Editor editor = context.getSharedPreferences(KEY,
                Context.MODE_PRIVATE).edit();
        editor.putString(REGISTRATION_KEY, registration);
        editor.commit();
        Toast.makeText(context, "Registration Id:" + registration,
                Toast.LENGTH_SHORT).show();
        new gcmstoreddata().Load_Data("REGKEY", registration, context);

    }
}

}

这是我的AndroidManifest.xml代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<permission
    android:name="com.test.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.test.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name="com.test.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.test.SplashScreen"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:noHistory="true"
        android:theme="@style/NoTitleBarFullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.test.gcm.GcmReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.test" />
        </intent-filter>
    </receiver>
</application>

这是我在logcat中的错误

04-11 14:48:04.396: I/GCM Request(3855): GCM Request
04-11 14:48:34.625: I/onReceive(3855): onReceive
04-11 14:48:34.625: I/REGISTRATION(3855): REGISTRATION
04-11 14:48:34.625: D/c2dm(3855): registration failed
04-11 14:48:34.625: D/c2dm(3855): SERVICE_NOT_AVAILABLE

0 个答案:

没有答案