我想将gcm客户端实现到现有的Android应用程序中。因此,通过以下this教程,我编写了以下代码:
public class RegisterForGCMAsyncTask extends AbstractSecureOperationTask {
...
@Override
protected Boolean doInBackground(String... params) {
String token = authenticate();
getRegId();
if (TextUtils.isEmpty(registrationId)) {
return false;
}
//
try {
URL url = convertToURLEscapingIllegalCharacters(String.format(Constants.REGISTER_URL,
registrationId, userId, token));
URLConnection connection = url.openConnection();
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
JSONParser parser = new JSONParser();
JSONObject rootObj = (JSONObject) parser.parse(streamReader);
String status = rootObj.get("status").toString();
if (status.equals("OK")) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private void getRegId() {
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
registrationId = gcm.register(PROJECT_ID);
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
}
}
AndroidMainfest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.xxxxxxxx.xxxxxxxx"
android:versionCode="20140617"
android:versionName="2.0.0">
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<permission
android:name="de.xxxxxxxx.xxxxxxxx.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="de.xxxxxxxx.xxxxxxxx.permission.MAPS_RECEIVE"/>
<permission
android:name="de.xxxxxxxx.xxxxxxxx.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="de.xxxxxxxx.xxxxxxxx.permission.C2D_MESSAGE"/>
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="de.retterapps.Handyalarm.views.activities.MainActivity"
android:configChanges="orientation|screenLayout|screenSize|layoutDirection"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".helper.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="de.xxxxxxxx.xxxxxxxx"/>
</intent-filter>
</receiver>
<service android:name=".helper.gcm.GcmMessageHandler"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxx"/>
</application>
昨天一切正常,我可以检索注册ID。但现在,总是IOException
说SERVICE_NOT_AVAILABLE
。
我在三星Galaxy S5和Genymotion仿真器(Android 4.1.2)上进行了测试,但结果总是相同。有谁知道如何解决问题?
这里是完整的堆栈跟踪:
java.io.IOException: SERVICE_NOT_AVAILABLE
at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
at de.retterapps.Handyalarm.helper.tasks.RegisterForGCMAsyncTask.getRegId(RegisterForGCMAsyncTask.java:72)
at de.retterapps.Handyalarm.helper.tasks.RegisterForGCMAsyncTask.doInBackground(RegisterForGCMAsyncTask.java:43)
at de.retterapps.Handyalarm.helper.tasks.RegisterForGCMAsyncTask.doInBackground(RegisterForGCMAsyncTask.java:24)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:27)
检查设备的时间。 Google无法在错误的时间内注册您的设备
答案 1 :(得分:10)
当我遇到这个问题时,对我有用的是打开Google Play应用程序。
在任何GCM注册尝试之前,设备重新启动后,它似乎必须至少启动一次。
答案 2 :(得分:4)
这很奇怪。我通过将GCM代码移动到onCreate()方法的末尾来解决它。
我的猜测是当我尝试注册CGM时,应用程序Context尚未完全创建。它需要一个有效的上下文。
答案 3 :(得分:0)
昨天我自己解决了这个问题。我按照演示应用程序将registerInBackground()中的AsyncTask更改为普通的java线程。这是代码:
private void registerInBackground() {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
Toast.makeText(context, getString(R.string.txt_gcm_registered), Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(context, getString(R.string.error_register_gcm), Toast.LENGTH_LONG).show();
break;
}
super.handleMessage(msg);
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regId = gcm.register(Constants.SENDER_ID);
if (sendRegistrationIdToBackend(regId)) {
handler.sendEmptyMessage(0);
} else {
handler.sendEmptyMessage(1);
}
storeRegistrationId(context, regId);
} catch (IOException ex) {
handler.sendEmptyMessage(1);
Log.e(LOG_TAG, ex.getMessage(), ex);
}
}
};
new Thread(runnable).start();
}
现在,它正在运作,但我不知道为什么。谁能解释一下原因?谢谢你的帮助!
答案 4 :(得分:0)
如果您在模拟器中运行应用,则必须安装Google Play服务。
我最近从Google原生模拟器切换到GenyMotion Emulator并遇到此错误。后来我意识到GenyMotion图像没有附带Google Play服务。安装完成后,问题解决了。