java 是否有 GCM registrationId 模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12403628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:45:15  来源:igfitidea点击:

Is there a GCM registrationId pattern?

javaandroidandroid-c2dmgoogle-cloud-messaging

提问by Sebastien Lorber

/**
 * @author Sebastien Lorber <i>([email protected])</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);
    }


}

Is there any known pattern for the GCM registrationId i can use to validate on application that the registrationId has a correct shape? I would just like to know which range of chars it has, which is the minimum and maximum size for exemple... or any other information...

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

回答by HitOdessit

I hasn't seen any official information about format of GCM registrationId, but I've analyzed our database of such IDs and can make following conclusions:

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

  • in most cases length of a registrationID equals 162symbols, but can be variations to 119symbols, maybe other lengths too;
  • it consists only from this chars: [0-9a-zA-Z\-\_]*
  • every regID contains one or both of "delimiters": - (minus) or _ (underline)
  • 在大多数情况下,registrationID 的长度等于162 个符号,但可以是119 个符号的变体,也可能是其他长度;
  • 它仅由以下字符组成: [0-9a-zA-Z\-\_]*
  • 每个 regID 都包含一个或两个“分隔符”:-(减号)或 _(下划线)

回答by Trevor Johns

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 anypattern, 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).

与“registration_id”字段一样,大小的上限是 cookie 的最大大小,即 4K(4096 字节)。