GCM服务不可用

时间:2015-02-04 02:21:29

标签: java android google-cloud-messaging

我按照http://hmkcode.com/android-google-cloud-messaging-tutorial/的例子,每件事都很好,但总是回应SERVICE_NOT_AVALIBLE 设备时间设置正确,包名也正确。 在这种情况下,任何人都可以帮助我

我的MainActivity.java

Button btnRegId;
EditText etRegId;
GoogleCloudMessaging gcm;
String regid;
String PROJECT_NUMBER = "943411953393";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnRegId = (Button) findViewById(R.id.btnGetRegId);
    etRegId = (EditText) findViewById(R.id.etRegId);

    btnRegId.setOnClickListener(this);
}
public void getRegId(){
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                }
                regid = gcm.register(PROJECT_NUMBER);
                msg = "Device registered, registration ID=" + regid;
                Log.i("GCM",  msg);

            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();

            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            etRegId.setText(msg + "\n");
        }
    }.execute(null, null, null);
}
@Override
public void onClick(View v) {
    getRegId();
}

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gmc"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

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

    <uses-permission android:name="com.example.gmc.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.gmc" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmMessageHandler" />

        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

试试这个

@Override
protected String doInBackground(Void... params) {
     String msg = "";
     int backoff = 2000;
     try {
          if (gcm == null) {
               gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
          }
     }
     catch (IOException ex) { 
          ex.printStackTrace()
     }
     for(int i=0;i<5;i++){
          try {
               regid = gcm.register(PROJECT_NUMBER);
               msg = "Device registered, registration ID=" + regid;
               Log.i("GCM",  msg);
               break;
          } 
          catch (IOException ex) {
               msg = "Error :" + ex.getMessage();
               backoff =backoff * 2;
               Thread.sleep(backoff);
          }
     }
     return msg;
}

你需要尝试多次尝试,增加每次尝试的等待时间。