Joã*_*hin 10 android google-cloud-messaging
这真让我讨厌.我有一个使用GCM的应用程序.我有人抱怨他们因错误而无法登录.让我告诉你我是如何进行登录的:
// Login Activity
//....
public void onClickLoginButton (View v) {
// Do some stuff...
new GCMRegister().execute(); //AsyncTask
}
private class GCMRegister extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground (Void... params) {
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
try {
//Get GCM Registration key
registrationId = googleCloud.register(Settings.PROJECT_NUMBER);
} catch (Exception e) {
Log.e("GCMRegister", e.toString());
return -1;
}
return 1;
} else {
return -3;
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
new LoginAsync().execute(); // Another AsyncTask to check in my database if user is valid and save its key
} else {
user.setEnabled(true);
password.setEnabled(true);
login.setEnabled(true);
if (result == -1) {
Toast.makeText(Login.this, "A problem occured login in", Toast.LENGTH_LONG).show();
} else if (result == -3) {
Toast.makeText(Login.this, "I need an internet connection", Toast.LENGTH_LONG).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以很多人抱怨他们因为"登陆时出现问题"错误而无法登录,这表明GCM登记失败.即使我自己使用我的设备Android 4.4.2,也不能一开始就做到这一点.我需要尝试登录2到3次,直到它工作(并且我的连接很好).我的LogCat上的错误是:
08-04 21:29:10.922: E/GCMRegister(18182): java.io.IOException: SERVICE_NOT_AVAILABLE
Run Code Online (Sandbox Code Playgroud)
那我的代码有什么问题?这让我疯了.
Nis*_*hia 16
我们遇到了同样的问题,用户无法第一次登录,但无论网络速度如何,都可以在第二次或第三次登录后登录.
我们通过Thread.sleep多次使用和重试来完成解决方法,直到收到GCM ID.
int noOfAttemptsAllowed = 5; // Number of Retries allowed
int noOfAttempts = 0; // Number of tries done
bool stopFetching = false; // Flag to denote if it has to be retried or not
String regId = "";
while (!stopFetching)
{
noOfAttempts ++;
GCMRegistrar.register(getApplicationContext(), "XXXX_SOME_KEY_XXXX");
try
{
// Leave some time here for the register to be
// registered before going to the next line
Thread.sleep(2000); // Set this timing based on trial.
} catch (InterruptedException e) {
e.printStackTrace();
}
try
{
// Get the registration ID
regId = GCMRegistrar.getRegistrationId(LoginActivity.this);
} catch (Exception e) {}
if (!regId.isEmpty() || noOfAttempts > noOfAttemptsAllowed)
{
// If registration ID obtained or No Of tries exceeded, stop fetching
stopFetching = true;
}
if (!regId.isEmpty())
{
// If registration ID Obtained, save to shared preferences
saveRegIDToSharedPreferences();
}
}
Run Code Online (Sandbox Code Playgroud)
注:该Thread.sleep和noOfAttemptsAllowed基于您的设计和其它参数可以玩耍了.我们的睡眠时间为7000,因此首次登记的可能性更高.但是,如果失败,下一次尝试将消耗另外7000毫秒.这可能会导致用户认为您的应用很慢.因此,使用这两个值智能地玩游戏.
小智 5
我遇到过同样的问题.我的问题是我是在我的活动的onCreate方法上做的.我发现上下文没有完全实现导致gcm无法成功注册.我提供了getApplicationContext()来获取GCM实例,这能够解决我的问题.
例:
使用活动的上下文引起了一个问题
GoogleCloudMessaging.getInstance(ActivityName.this)
Run Code Online (Sandbox Code Playgroud)
使用应用程序上下文解决了我
GoogleCloudMessaging.getInstance(getApplicationContext())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12638 次 |
| 最近记录: |