GCM错误服务无法提供

时间:2014-07-21 21:21:18

标签: android google-cloud-messaging

我在注册时遇到问题 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;
}
}

7 个答案:

答案 0 :(得分:1)

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

2.在移动设备上卸载应用程序。

3.清理并运行项目。

4.这有用!!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

基于文档:

  

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

服务不可用主要是由于网络问题。您将不得不重试注册服务。请参阅我之前的回答,以便重试注册服务: https://stackoverflow.com/a/25183437/3492139

答案 6 :(得分:-1)

如果有人做了所有事情并仍然面临这个问题然后只需从您的项目中删除appcompcat7库它也可以帮助您。