Google云消息传递 - GCM - SERVICE_NOT_AVAILABLE

Tho*_*ard 23 android google-cloud-messaging service-not-available

我正在尝试,为了实施新的GCM,我正在关注Google的说法:http: //developer.android.com/guide/google/gcm/gs.html

我在获取设备注册ID时遇到困难!

我的应用不断尝试与Google服务器连接,这里我的错误日志:

onReceive: com.google.android.gcm.intent.RETRY
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
Registering app com.dombox.app of senders _my_sender_id_
Releasing wakelock
onReceive: com.google.android.c2dm.intent.REGISTRATION
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
Registration error: SERVICE_NOT_AVAILABLE
Scheduling registration retry, backoff = 912617 (768000)
Releasing wakelock
Run Code Online (Sandbox Code Playgroud)

这是我的活动代码,要求提供ID:

    try{
        Log.i(TAG, "[checkNotifRegistration] checkDevice");
        GCMRegistrar.checkDevice(this);
        Log.i(TAG, "[checkNotifRegistration] checkManifest");
        GCMRegistrar.checkManifest(this);
        if (GCMRegistrar.isRegistered(this)) {
            Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
        }
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            // SENDER_ID is my project id into google account url
            GCMRegistrar.register(this, SENDER_ID);
            Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
        } else {
            Log.i(TAG, "[checkNotifRegistration] already registered as : " + regId);
        }
    } catch(Exception e){
        Log.e(TAG, "[checkNotifRegistration] Exception : "+e.getMessage());
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的服务代码

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(SENDER_ID);
           // SENDER_ID is my project id into google account url
    // TODO Auto-generated constructor stub
    Log.d(TAG, "[GCMIntentService] start");
}


public GCMIntentService(String senderId) {
    super(senderId);
    // TODO Auto-generated constructor stub
    Log.d(TAG, "[GCMIntentService] start - sender Id : "+senderId);
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的Android Manifest:

<permission android:name="com.dombox.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.dombox.app.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" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

    <receiver
        android:name="com.google.android.gcm.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.myapp.app" />
        </intent-filter>
    </receiver>

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

    <activity
        android:name=".activity.Myapp"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
Run Code Online (Sandbox Code Playgroud)

我正在按照Google的说明操作,但我遇到了这个SERVICE_NOT_AVAILABLE错误,

我究竟做错了什么 ?

Tho*_*ard 39

问题不是代码相关,而是链接到测试手机. 测试手机没有SIM卡,时钟未设置.所以谷歌不能在错误的时间重新贴上它.

我设法使用Android虚拟设备,谷歌api和设置测试电话时钟注册ID .


Rya*_*ray 7

android:enabled="true"  
Run Code Online (Sandbox Code Playgroud)

删除这个^

如果您尚未将其添加到清单中

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8"
Run Code Online (Sandbox Code Playgroud)

您不需要GCMIntentService中的第二个构造函数.只有这一个

public GCMIntentService() {         
 super(SENDER_ID);  }
Run Code Online (Sandbox Code Playgroud)

当您浏览Google代码网站并浏览GCM服务时,请确保您的SENDER_ID是从浏览器中复制的网址数.