GCM error SERVICE NOT AVAILABLE

丶灬走出姿态 提交于 2019-12-30 11:20:12

问题


I'm have a problem on moment of register on GCM server, a message is show in my log: SERVICE NOT AVAILABLE. I tried several option, but not get good results:

Already enable the dependency of google_play_service, Already check the time ofclock, time zone set with Brasilia, Already check all I know

Follows below my code.

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

回答1:


1.You make sure class GetRegisterGCM() have method serviceGCM.getRegistrationId() in package br.com.clientechattcc not in br.com.clientechattcc.asynctask;

2.Uninstall application on mobile.

3.Clean and Run Project.

4.That's Works!!




回答2:


I had same problem. I closed Wifi and I tried again with 3g. It is solved my problem. When I opened the Wifi, GCM worked again.




回答3:


You need to have stable internet connection. Restart your emulator and it will work.




回答4:


If you're sure that all settings are correct, then it might be possible that port 5228 has been blocked on your connection. Switch to mobile network in that case.

Many corporate networks block these ports (5228-5230) used by GCM registration.




回答5:


I tried all the suggestions here with no luck. Then I rebooted my phone and it started to work.




回答6:


Based on Documentation:

When the application receives a com.google.android.c2dm.intent.REGISTRATION intent with the error extra set as SERVICE_NOT_AVAILABLE, it should retry the failed operation (register or unregister).

Service Not available is mainly due to network issue. You will have to Retry the register service. Refer my previous answer here for Retrying register service: https://stackoverflow.com/a/25183437/3492139




回答7:


If Someone Do All The Things and Still Facing This issue Then Just Remove appcompcat7 Library From Your Project It Helps It may help you also.



来源:https://stackoverflow.com/questions/24875009/gcm-error-service-not-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!