【问题标题】:GCM error SERVICE NOT AVAILABLEGCM 错误服务不可用
【发布时间】:2015-08-17 20:33:21
【问题描述】:

我在注册时遇到问题 GCM 服务器,我的日志中显示一条消息:SERVICE NOT AVAILABLE。我尝试了几个选项,但没有得到好的结果:

已经启用 google_play_service 的依赖, 已经检查了时钟的时间,与巴西利亚设置的时区, 已经检查了我所知道的一切

遵循我的代码。

<?xml version="1.0" encoding="utf-8"?>

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

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

<permission android:name="br.com.clientechattcc.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="br.com.clientechattcc.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

     <!-- android:name=".appConfig.GCMBroadcastReceiver" -->
    <receiver
        android:name=".appConfig.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
           <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
           <action android:name="com.google.android.c2dm.intent.RECEIVE" />
           <action android:name="com.google.android.c2dm.SEND" />
           <category android:name="br.com.clientechattcc" />
        </intent-filter>
   </receiver>

   <service android:name=".appConfig.GCMIntentService" />

    <activity
        android:name="br.com.clientechattcc.activity.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>

    <activity
        android:name="br.com.clientechattcc.activity.CadastrarUsuarioActivity">
    </activity>

    <activity
       android:name="br.com.clientechattcc.activity.HomeActivity">
    </activity>

</application>

package br.com.clientechattcc.configgcm;
import br.com.clientechattcc.appConfig.AppConf;
import br.com.clientechattcc.enuns.GCM;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Log.i(AppConf.TAGLOG, GCM.ONRECEIVE.toString());

    ComponentName comp = new ComponentName(context.getPackageName(), 
                                            GCMIntentService.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);

}
}

package br.com.clientechattcc.configgcm;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import br.com.clientechattcc.appConfig.AppConf;
import br.com.clientechattcc.enuns.GCM;
import br.com.clientechattcc.notify.CreateNotification;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMIntentService extends IntentService {

public GCMIntentService() {
     super(GCM.GCMINTNVTSERVICE.toString());
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(AppConf.TAGLOG, GCM.ONHANDLEINTET.toString());

    CreateNotification notification = new CreateNotification();
    Bundle extras = intent.getExtras();

    GoogleCloudMessaging gcm = AppConf.getGCM();
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) { 

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            notification.sendNotification("Send error: " + extras.toString(), this);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            notification.sendNotification("Deleted messages on server: " + extras.toString(),this);
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {

            notification.sendNotification("OK!!!!!!!!!!!!!" + extras.toString(),this);
            Log.i(AppConf.TAGLOG, "Received: " + extras.toString());
        }
    }

    GCMBroadcastReceiver.completeWakefulIntent(intent);
}
}

package br.com.clientechattcc.asynctask;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import br.com.clientechattcc.appConfig.AppConf;
import br.com.clientechattcc.configgcm.ServiceGCM;
import br.com.clientechattcc.enuns.MessageApp;
import br.com.clientechattcc.serverrequest.RequestClientUsuario;

public class GetRegisterGCM  extends AsyncTask<Void, Void, String> {

private Activity activity;

public GetRegisterGCM(Activity activity) {
    this.activity = activity;
}

@Override
protected String doInBackground(Void... params) {

     String msg = "";
     String regid = ""; 
     try { 

        ServiceGCM serviceGCM = new ServiceGCM();

        regid = serviceGCM.getRegistrationId(activity.getApplicationContext());

        if(regid.equals("")){
             //AppConf.getGCM()
            regid = GoogleCloudMessaging.getInstance(activity.getApplicationContext()).register(AppConf.SENDER_ID);

            serviceGCM.storeRegistrationId(activity.getApplicationContext(), regid);
        }

        RequestClientUsuario client = new RequestClientUsuario();

        client.updateGCM(regid, AppConf.getDataUsuario().getUsuario());

        if(!client.getStatus())
          msg = client.getRetorno();

     } catch (Exception ex) {
        msg = MessageApp.ERRORAPP+ex.getMessage()+" / "+this.getClass().getName();
        Log.i(AppConf.TAGLOG, msg);
    }
    return msg;
}
}

【问题讨论】:

  • 请看链接的问题,我相信你只需要重试失败的操作。否则,您可能需要向 Google 开票。
  • 我试过了,但没有效果。如何在谷歌支持上开票?
  • 看起来 GCM 可能没有合适的票务系统。看到这个线程groups.google.com/forum/#!searchin/android-gcm/… 看起来很多其他人由于不同的原因遇到了同样的问题。该论坛可能是询问您是否在 SO 中找不到答案的地方。
  • 谢谢史蒂夫,服务可能停止了吗?

标签: android google-cloud-messaging


【解决方案1】:

1.确保类 GetRegisterGCM() 在包 br.com.clientechattcc 中没有方法 serviceGCM.getRegistrationId() 不在 br.com.clientechattcc.asynctask 中;

2.在手机上卸载应用程序。

3.清理并运行项目。

4. 成功了!!

【讨论】:

    【解决方案2】:

    我有同样的问题。我关闭了 Wifi,然后用 3g 再次尝试。它解决了我的问题。 当我打开 Wifi 时,GCM 又开始工作了。

    【讨论】:

    • 我的手机上没有 SIM 卡,该怎么办?
    【解决方案3】:

    您需要有稳定的互联网连接。重新启动你的模拟器,它会工作。

    【讨论】:

      【解决方案4】:

      如果您确定所有设置都正确,则可能是您的连接上的端口 5228 已被阻止。在这种情况下切换到移动网络。

      许多公司网络会阻止 GCM 注册使用的这些端口 (5228-5230)。

      【讨论】:

        【解决方案5】:

        我在这里尝试了所有建议,但没有成功。然后我重新启动了手机,它开始工作了。

        【讨论】:

          【解决方案6】:

          基于文档:

          当应用程序收到一个 com.google.android.c2dm.intent.REGISTRATION 意图错误 额外设置为SERVICE_NOT_AVAILABLE,它应该重试失败 操作(注册或注销)。

          服务不可用主要是由于网络问题。您将不得不重试注册服务。请在此处参考我之前的答案以获取重试注册服务: https://stackoverflow.com/a/25183437/3492139

          【讨论】:

            【解决方案7】:

            如果有人做了所有事情但仍然面临这个问题,那么只需从您的项目中删除 appcompcat7 库,它会有所帮助,它也可能对您有所帮助。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-01-14
              • 1970-01-01
              • 2021-02-21
              • 2021-01-07
              • 1970-01-01
              相关资源
              最近更新 更多