Android GCM 一些设备无法注册

7

我在Google Play上有一个应用程序,已经下载了5000次。

其中有900个用户未能在我的服务器上存储GCM注册ID。

我担心这18%的失败率,并且很难找出原因。肯定不是将ID传递给服务器和存储的问题,所以我认为这一定是注册失败了。有没有人在他们的项目中遇到过类似的情况?代码如下:

// Fired every time the app is booted in Main Activity
private void setupGCM()
{
    String gcmRegId = GCMRegistrar.getRegistrationId(this);
    if (gcmRegId.equals(""))
    {
        GCMRegistrar.register(this, AppProperties.GCM_SENDER_ID);
    } else
    {
        // This is well tested and is not the issue
        ServerUtils.sUpdatePushToken(this, gcmRegId);
    }
}

// GCMIntentService Class onRegistered
@Override
protected void onRegistered(Context context, String registrationId)
{
    mContext = context;
    if (!registrationId.equals(""))
    {
        AppProperties.GCM_REGISTRATION_ID = registrationId;
        new UpdateGCMRegistrationIdAsync().execute();
    } else
    {
        GCMRegistrar.setRegisteredOnServer(context, false);
    }
}

// GCMIntentService Class  My Async class for updating Reg Id
private class UpdateGCMRegistrationIdAsync extends AsyncTask<Integer, Integer, Void>
{
    @Override
    protected Void doInBackground(Integer... params)
    {           
        ServerUtils.sUpdatePushToken(mContext, AppProperties.GCM_REGISTRATION_ID);
        return null;
    }
}

不建议使用.equals("")。请尝试使用regId.trim().length() > 0。 - drulabs
1
感谢您的回答,不过根据 Google 在 GCM 演示文稿中记录,应该使用 .equals("")。此外,这适用于 4100 设备而非 900 设备吗? - TommyGuns21
顺便提一下,我最初使用了regId.trim().length()。然后出现了同样的间歇性问题。 - TommyGuns21
4个回答

4

我刚刚解决了一个问题,这个问题可能和你的问题一样。Android版本低于4.1的设备无法注册GCM。以下是我在android-gcm google组中发布的帖子:

Ok, got it solved. Thanks to J.Carlos Navea and Mark Murphy in this thread:
https://groups.google.com/forum/?fromgroups=#!searchin/android-gcm/onRegister/android-gcm/CqMRN4gVRpY/AGphcX1AuhgJ

The issue was the <category> tag in the receiver declaration. It is required for anything less than 4.1
Use your package name there. Additionally, check to make sure that the <permission> and <uses-permission>
tags use the same string/name. I was getting away with that one as well on 4.1

2

有一些原因会导致GCM无法正常工作,例如:

未同步的Google账户。

在Android 3.2及以上版本中,当用户“强制停止”应用程序时,GCM也无法正常工作。


1
感谢您的回答,Google帐户将被同步,因为他们需要从Google Play下载应用程序。就强制停止而言,如果这对18%的用户造成问题,我会感到惊讶。 - TommyGuns21
以下是我的一些问题,用户可能更改同步的帐户,现在不确定,但它会使“注册ID”更改,您也必须在Web服务中进行更改。而且您应该考虑到900个用户可能并不是新用户。 - Guilherme Gregores

1

你是否处理了GCM注册失败的情况?如果你正在使用GCMIntentService,请确保你实现了以下内容:

@Override
protected void onError(Context context, String regid) {
   // Handle error condition.
}

0

我曾经遇到过同样的问题,有些设备没有注册成功。我检查了我的清单文件,发现包名有问题,请仔细检查。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接