GCM错误服务不可用 [英] GCM error SERVICE NOT AVAILABLE

查看:177
本文介绍了GCM错误服务不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对注册于当下的问题 GCM服务器,消息显示在我的日志:服务不可用。我尝试了几种选择,但没有得到很好的效果:

已启用google_play_service的依赖,   已检查的时候ofclock,时区设置巴西利亚,   已检查所有我知道

跟随我下面的code。

 < XML版本=1.0编码=UTF-8&GT?;

<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=br.com.clientechattcc
安卓版code =1
机器人:VERSIONNAME =1.0>

<用途-SDK
    安卓的minSdkVersion =14
    机器人:targetSdkVersion =21/>

<允许机器人:名称=br.com.clientechattcc.permission.C2D_MESSAGE
    安卓的ProtectionLevel =签名/>
<使用-权限的Andr​​oid:名称=br.com.clientechattcc.permission.C2D_MESSAGE/>
<使用-权限的Andr​​oid:名称=com.google.android.c2dm.permission.RECEIVE/>
<使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_SETTINGS/>
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_WIFI_STATE/>
<使用-权限的Andr​​oid:名称=android.permission.GET_ACCOUNTS/>

<应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>

    <元数据的android:NAME =com.google.android.gms.version
    机器人:值=@整数/ google_play_services_version/>

     <  - 机器人:名称=。appConfig.GCMBroadcastReceiver - >
    <接收器
        机器人:名称=。appConfig.GCMBroadcastReceiver
        机器人:权限=com.google.android.c2dm.permission.SEND>
        <意向滤光器>
           <作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
           <作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
           <作用机器人:名称=com.google.android.c2dm.SEND/>
           <类机器人:名称=br.com.clientechattcc/>
        &所述; /意图滤光器>
   < /接收器>

   <服务机器人:名称=。appConfig.GCMIntentService/>

    <活动
        机器人:名称=br.com.clientechattcc.activity.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;

    <活动
        机器人:名称=br.com.clientechattcc.activity.CadastrarUsuarioActivity>
    < /活性GT;

    <活动
       机器人:名称=br.com.clientechattcc.activity.HomeActivity>
    < /活性GT;

< /用途>
 

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

公共类GCMBroadcastReceiver扩展WakefulBroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){

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

    组件名补偿=新的组件名(context.getPackageName()
                                            GCMIntentService.class.getName());

    startWakefulService(上下文,(intent.setComponent(化合物)));
    的setResult code(Activity.RESULT_OK);

}
}
 


 包br.com.clientechattcc.configgcm;

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

进口com.google.android.gms.gcm.GoogleCloudMessaging;

公共类GCMIntentService扩展IntentService {

公共GCMIntentService(){
     超级(GCM.GCMINTNVTSERVICE.toString());
}

@覆盖
保护无效onHandleIntent(意向意图){
    Log.i(AppConf.TAGLOG,GCM.ONHANDLEINTET.toString());

    CreateNotification通知=新CreateNotification();
    捆绑额外= intent.getExtras();

    GoogleCloudMessaging GCM = AppConf.getGCM();
    字符串为messageType = gcm.getMessageType(意向);

    如果(!extras.isEmpty()){

        如果(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(为messageType)){
            notification.sendNotification(发送错误:+ extras.toString(),这一点);
        }否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(为messageType)){
            notification.sendNotification(在服务器上删除的邮件:+ extras.toString(),这一点);
        //如果这是一个普通的GCM消息,做了一些工作。
        }否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(为messageType)){

            notification.sendNotification(行!!!!!!!!!!!!!+ extras.toString(),这一点);
            Log.i(AppConf.TAGLOG,收到:+ extras.toString());
        }
    }

    GCMBroadcastReceiver.completeWakefulIntent(意向);
}
}
 


 包br.com.clientechattcc.asynctask;

进口com.google.android.gms.gcm.GoogleCloudMessaging;

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

公共类GetRegisterGCM扩展的AsyncTask<虚空,虚空,字符串> {

私人活动的活动;

公共GetRegisterGCM(活动活动){
    this.activity =活动;
}

@覆盖
保护字符串doInBackground(空... PARAMS){

     弦乐味精=;
     字符串REGID =;
     尝试 {

        ServiceGCM serviceGCM =新ServiceGCM();

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

        如果(regid.equals()){
             //AppConf.getGCM()
            REGID = GoogleCloudMessaging.getInstance(activity.getApplicationContext())寄存器(AppConf.SENDER_ID)。

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

        RequestClientUsuario客户端=新RequestClientUsuario();

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

        如果(!client.getStatus())
          味精= client.getRetorno();

     }赶上(例外前){
        味精= MessageApp.ERRORAPP + ex.getMessage()+/+ this.getClass()的getName()。
        Log.i(AppConf.TAGLOG,味精);
    }
    返回味精;
}
}
 

解决方案

1。你要确保一流的GetRegisterGCM()有方法serviceGCM.getRegistrationId()在不br.com.clientechattcc.asynctask包br.com.clientechattcc;

2.Uninstall的移动应用程序。

3.Clean和运行项目。

4.That的作品!

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.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!!

这篇关于GCM错误服务不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆