Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GCM服务不可用_Java_Android_Google Cloud Messaging - Fatal编程技术网

Java GCM服务不可用

Java GCM服务不可用,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,我举的例子是,一切都很好,但它总是提供响应服务 设备时间设置正确,包名称也正确。 有人能帮我吗 my MainActivity.java Button btnRegId; EditText etRegId; GoogleCloudMessaging gcm; String regid; String PROJECT_NUMBER = "943411953393"; @Override protected void onCreate(Bundle savedInstanceState) {

我举的例子是,一切都很好,但它总是提供响应服务 设备时间设置正确,包名称也正确。 有人能帮我吗

my 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>
试试这个

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

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

您在哪个设备上测试此功能?服务不可用通常发生在设备中的google play store不是最新的时候。尝试在你的设备上更新你的google play商店。我在华硕ZENPHONE 5上进行了测试,google play商店是最新的。请帮助我确认gcm是否发生错误。注册?是的,它发生了!!!你能告诉我为什么吗?你在开发者控制台中启用了GCM API了吗?它仍然不工作,logcat显示02-04 12:33:36.196:W/ActivityManager 92:无法启动服务意图{act=com.google.android.c2dm.Intent.REGISTER pkg=com.google.android.gms有额外功能}:没有找到注册ID?从您的清单中删除logcat中的错误,您的GCM消息似乎没有从意图中提取的额外内容。。从服务器发送邮件时,您是否在邮件中添加了附加密钥。什么是附加密钥?那是项目号吗?我想向GCM注册设备的id,所以我只需声明从sole.developers.google.com获得的项目编号。要获得完整的教程,请阅读本教程