我正在使用这里描述的使用GoogleCloudMessaging
类的新方法使用 gcm。但是 10 次中有 9 次给了我SERVICE_NOT_AVAILABLE
错误。只有一次它注册了一个设备。
07-01 14:24:55.907 2074-2090/com.deveyes.carrefour W/System.err: java.io.IOException: SERVICE_NOT_AVAILABLE
07-01 14:24:56.037 2074-2090/com.deveyes.carrefour W/System.err: at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
我的代码在我的活动中如下所示:
.....
gcmRegId = gcm.register(CarrefourConfig.GCM_SENDER_ID);
.....
后来我才知道我需要使用自动重试。我读过这个,但这个文件似乎很旧。与我们不再使用 IntentService 的新方式一样。还是我们?在 BroadcastReciever 中,我可以获取消息并通知用户。
我在这里做错了什么?
编辑:
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<supports-screens
android:xlargeScreens="false"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.location" android:required="true" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<permission
android:name="com.deveyes.carrefour.android.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.deveyes.carrefour.android.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.deveyes.carrefour.android.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Carrefour"
android:name=".CarrefourApplication">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCKznwauzk_rNdKf7Qw0rjpmf4AWchCRwI" />
<activity
android:name=".ui.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.deveyes.carrefour" />
</intent-filter>
</receiver>
<!--service android:name=".services.CarrefourLocationService" /-->
</application>
我的主要活动:
private void registerBackground() {
new AsyncTask(){
@Override
protected Object doInBackground(Object[] objects) {
try {
if(gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);
}
gcmRegId = gcm.register(CarrefourConfig.GCM_SENDER_ID);
Log.i(TAG,"regid:"+gcmRegId);
setRegistrationId(context,gcmRegId);
ServerUtilities.register(context,gcmRegId);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(null,null,null);
}