【问题标题】:GCM SERVICE_NOT_AVAILABLE errorGCM SERVICE_NOT_AVAILABLE 错误
【发布时间】:2013-12-18 15:53:53
【问题描述】:

我在我的 android 应用程序中使用 GCM,当我尝试使用 GCM 注册设备时(使用命令 gcm.register(SENDER_ID))我收到 SEVICE_NOT_AVAILABLE 错误,我尝试了有人建议的 here(已接受的解决方案)和我确实得到了注册 ID。

为什么会这样?

Android 清单:

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

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

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

    <uses-permission android:name="com.appspot.smartgan.permission.C2D_MESSAGE" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name="com.appspot.smartgan.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" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.appspot.smartgan" />
            </intent-filter>
        </receiver>

        <service 
            android:name=".GcmIntentService"
            android:enabled="true" />

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

        <activity
            android:name="com.appspot.smartgan.LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="adjustResize|stateVisible" >
        </activity>
        <activity
            android:name="com.appspot.smartgan.ChildActivity"
            android:label="@string/title_activity_child" >
        </activity>
    </application>

</manifest>

注册方法:

private void registerInBackground() {   
    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            String message = "";

            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                message = "Device registered, registration ID=" + regid;

                Log.d("SMARTGAN_PLAY", "register completed");

                // send registration id to the server
                sendRegistrationIdToServer();

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException e) {
                message = "Error:" + e.getMessage();
            }
            return message;
        }

    }.execute(null, null, null);
}

【问题讨论】:

    标签: java android google-cloud-messaging


    【解决方案1】:

    我认为问题在于您的 Google API 项目中提到的允许 IP 地址。 请检查项目的详细信息并删除restrict use to IP address 下的 IP 地址。我遇到了类似的问题。

    【讨论】:

      【解决方案2】:

      如果您收到注册 ID,则表示您已成功将设备注册到 GCM。

      编辑: 这是一个很好的 GCM 教程:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

      <receiver
              android:name="rockit.app.beardallstreetprimary.GCMBroadcastReceiver"
              android:permission="com.google.android.c2dm.permission.SEND" >
              <intent-filter>
                  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                  <category android:name="rockit.app.beardallstreetprimary" />
              </intent-filter>
          </receiver>
      
          <service android:name="com.google.android.gcm.GCMIntentService" android:enabled="true" />
      

      我的 GCM 是根据我上面链接的教程设置的,所以如果你也通读了它,它应该对你有用,看起来离你不远了。我会说 Manifest 非常挑剔,您需要确保完全链接您的 Packages。

      你也不需要注册意图

      【讨论】:

      • 我知道,但是当 gcm.register() 产生 IOException 时
      • 如果没有看到您的代码副本,我真的无法为您提供太多帮助,所以我确切地知道您将注册命令放在哪里。我已经编辑了我的答案,并提供了一个教程链接,该教程帮助我开始工作。如果你可以用你的代码编辑你的问题,我将无法提供更多帮助。
      • 调用注册函数的活动类很长,所以我不能在这里添加所有内容,但是我遵循了谷歌发布的示例项目here,所以你可以在那里看到我的代码一样。
      • 老实说,我们真的帮不了你,除非我们看到一些我们现在只是在黑暗中刺伤的代码。我觉得这可能是一个明显的事情,因为我所有的问题都源于在清单中错误地设置了接收器。
      • 我已经添加了 manifest 和 registerInBackground 函数
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      相关资源
      最近更新 更多