简体   繁体   English

GCM服务不可用

[英]GCM service not avalible

I follow the example of http://hmkcode.com/android-google-cloud-messaging-tutorial/ , every thing is fine but its alway response SERVICE_NOT_AVALIBLE The device time is correctly setup, package name is correct,too. 我遵循http://hmkcode.com/android-google-cloud-messaging-tutorial/的示例,一切正常,但始终响应Service_NOT_AVALIBLE设备时间设置正确,包名称也正确。 could anyone help me in this case 在这种情况下有人可以帮助我吗

my MainActivity.java 我的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>

try this 尝试这个

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

you need to try several attempts increasing the wait time for each try. 您需要尝试几次尝试,以增加每次尝试的等待时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM