3

I have been trying to find a way to target Huawei mobile devices so I can redirect them to the Huawei app store, but can't seem to find a solution. The one I have thought of so far is writing a regex to check for HUAWEI | Huawei | huawei in the string returned from the navigator.userAgent object.. but I am not sure if it will work for all such devices even though that is what i think looking at these sample userAgents

Is there an emulator or something like browserstack to try this out?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kolyo Peev
  • 145
  • 2
  • 11

4 Answers4

6

You can query the useragent to determine whether the current device is a Huawei phone.

Currently, Huawei browser provides UAs based on the HMS or GMS device type. The following are examples (the browser version number, HMS version number, GMS version number, and device information will change accordingly):

  1. Both the HMS and GMS are installed. The following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;HMSCore/4.0.0 GMS/10.4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

  1. If only the HMS is installed, the following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;HMSCore/4.0.0 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

  1. Only the GMS is installed. The following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;GMS/10.4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

  1. There is no HMS or GMS. The following is an example:

Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • this seems nice, but to be sure we are getting the Huawei devices I think the userAgent should be checked for the device model wich in the above case would be "LYA-AL00".. so there shoukd be a polyfill for that or something ? – Kolyo Peev Feb 05 '21 at 05:51
  • hi@Кольо Пеев,you can get brand information from ro.product.brand, normally the value will be HUAWEI or HONOR for huawei phones. – zhangxaochen Feb 18 '21 at 08:28
  • 2
    @Кольо Пеев, if it's about checking the app store compatibility, this is a very good answer as if you can ensure the phone has HMSCore and not GMS then you should redirect to App Gallery instead of Google Play – ceyquem Apr 01 '21 at 16:24
2

I don't know about targeting a specific type of mobile phone, but you can test the user agent string:

/**
 * Determine the mobile operating system.
 * This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
 *
 * @returns {String}
 */
function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}
0

Simplest and shortest checking method: (Tablets excluded)

navigator.userAgent.match(/"ALP-"|"AMN-"|"ANA-"|"ANE-"|"ANG-"|"AQM-"|"ARS-"|"ART-"|"ATU-"|"BAC-"|"BLA-"|"BRQ-"|"CAG-"|"CAM-"|"CAN-"|"CAZ-"|"CDL-"|"CDY-"|"CLT-"|"CRO-"|"CUN-"|"DIG-"|"DRA-"|"DUA-"|"DUB-"|"DVC-"|"ELE-"|"ELS-"|"EML-"|"EVA-"|"EVR-"|"FIG-"|"FLA-"|"FRL-"|"GLK-"|"HMA-"|"HW-"|"HWI-"|"INE-"|"JAT-"|"JEF-"|"JER-"|"JKM-"|"JNY-"|"JSC-"|"LDN-"|"LIO-"|"LON-"|"LUA-"|"LYA-"|"LYO-"|"MAR-"|"MED-"|"MHA-"|"MLA-"|"MRD-"|"MYA-"|"NCE-"|"NEO-"|"NOH-"|"NOP-"|"OCE-"|"PAR-"|"PIC-"|"POT-"|"PPA-"|"PRA-"|"RNE-"|"SEA-"|"SLA-"|"SNE-"|"SPN-"|"STK-"|"TAH-"|"TAS-"|"TET-"|"TRT-"|"VCE-"|"VIE-"|"VKY-"|"VNS-"|"VOG-"|"VTR-"|"WAS-"|"WKG-"|"WLZ-"|"YAL"/i)

This method uses prefixes and dashes as fingerprint from the device id list provided in this github repo. to detect Huawei devices.

ie. Huawei P Smart 2019 device ids: "POT-LX1AF","POT-LX2J","POT-LX1RUA","POT-LX3" list, but used POT- for detection. Note: We are assuming that, this prefix is unique for Huawei. If a device with a different brand with same prefix sends the device id will also be count as Huawei.

Note about "HUAWEI" string check method, checking for this string does not guarantee if the device is Huawei.

If the visitor using a third party browser like Chrome, user-agent sent does not contain this string, It is sent when the visitor comes from an in-app browser view like, Twitter or Instagram (when you click to a link in these apps, the web site opens in a web view).It is also sent with visits from some apps like Petal Search app (Huawei's official search engine app)

trkaplan
  • 3,217
  • 2
  • 23
  • 26
0

This solution working on my nginx server. I made page /download/ for PC. In link /downloads/ server redirect users.

  location /downloads/ {
    if ($http_user_agent ~* '(iPhone|iPod|blackberry)') {
      return 301 https://apps.apple.com/us/app/man-errors/id1581826209;
          }

    if ($http_user_agent ~* '(huawei)') {
      return 301 https://appgallery.huawei.com/app/C105589651;
          }

    if ($http_user_agent ~* '(android)') {
      return 301 https://play.google.com/store/apps/details?id=com.manerrors.truck;
          }
    return 301 /download/;
  }