7
/**
 * @author Sebastien Lorber <i>(lorber.sebastien@gmail.com)</i>
 */
public enum EnumDeviceType {

    ANDROID {
        @Override
        public boolean validateDeviceIdentifier(String deviceIdentifier) {
            Preconditions.checkArgument( !Strings.isNullOrEmpty(deviceIdentifier) );
            return ANDROID_REGISTRATION_ID_PATTERN.matcher(deviceIdentifier).matches();
        }
    },
    IOS {
        @Override
        public boolean validateDeviceIdentifier(String deviceIdentifier) {
            Preconditions.checkArgument( !Strings.isNullOrEmpty(deviceIdentifier) );
            return IOS_DEVICE_TOKEN_PATTERN.matcher(deviceIdentifier).matches();
        }
    },
    ;

    // TODO how do we validate registration Ids
    public static final Pattern ANDROID_REGISTRATION_ID_PATTERN = Pattern.compile(".*");
    // IOS device token is a 64 HEX string
    public static final Pattern IOS_DEVICE_TOKEN_PATTERN = Pattern.compile("[a-fA-F0-9]{64,64}");


    public abstract boolean validateDeviceIdentifier(String deviceIdentifier);


    public boolean isIos() {
        return IOS.equals(this);
    }

    public boolean isAndroid() {
        return ANDROID.equals(this);
    }


}

是否有任何已知的 GCM registrationId 模式可以用来在应用程序上验证registrationId 具有正确的形状?我只想知道它有哪些字符范围,例如最小和最大大小......或任何其他信息......

4

2 回答 2

17

我没有看到任何关于 GCM 注册 ID 格式的官方信息,但我分析了我们的此类 ID 数据库,可以得出以下结论:

  • 在大多数情况下,registrationID 的长度等于162 个符号,但可以变化为119 个符号,也可能是其他长度;
  • 它仅由以下字符组成:[0-9a-zA-Z\-\_]*
  • 每个 regID 都包含一个或两个“分隔符”:-(减号)或 _(下划线)
于 2012-09-13T11:15:36.087 回答
14

The documentation doesn't specify any pattern, therefore any valid string is allowed. The format may change in the future; please do not validate this input against any pattern, as this may cause your app to break if this happens.

As with the "registration_id" field, the upper bound on size is the max size for a cookie, which is 4K (4096 bytes).

于 2012-09-19T20:27:14.810 回答