Viber 为桌面和移动设备使用不同的链接。
对于移动设备:
<a href="viber://chat?number=PHONE_WITHOUT_PLUS">Text to Viber</a>
<a href="viber://add?number=PHONE_WITHOUT_PLUS">Add the phone to Viber</a>
对于桌面设备:
<a href="viber://chat?number=+PHONE_WITH_PLUS">Text to Viber</a>
<!-- or use %2B = + -->
<a href="viber://chat?number=%2BPHONE_WITH_PLUS">Text to Viber</a>
应该安装 Viber。
在服务器代码中,需要通过用户代理检测用户设备的类型(台式机或移动设备)或使用 CSS 规则(例如,@min-width()
)或 JS 来显示必要的链接。
PHP(没有任何库):
function isMobile($userAgent) {
$userAgent = strtolower($userAgent);
$mobileAgents = array('ipad', 'iphone', 'android', 'pocket', 'palm', 'windows ce', 'windowsce', 'cellphone', 'opera mobi', 'ipod', 'small', 'sharp', 'sonyericsson', 'symbian', 'opera mini', 'nokia', 'htc_', 'samsung', 'motorola', 'smartphone', 'blackberry', 'playstation portable', 'tablet browser');
foreach ($mobileAgents as $value) {
if (strpos($userAgent, $value) !== false) return true;
};
return false;
};
// Gets a user agent from the server variable
$agent = $_SERVER['HTTP_USER_AGENT'];
// Gets a user agent from Laravel Request
$agent = request()->userAgent();
$mobile = isMobile($agent);
// Simple PHP output
<?php if($mobile): ?>
<a href="viber://chat?number=PHONE_WITHOUT_PLUS">Text to Viber</a>
<?php else: ?>
<a href="viber://chat?number=+PHONE_WITH_PLUS">Text to Viber</a>
<?php endif; ?>
带有 Twitter Bootstrap v5 的HTML 和 CSS(大 lg ≥992px):
<a href="viber://chat?number=PHONE_WITHOUT_PLUS" class="d-lg-none">Text to Viber (Mobile)</a>
<a href="viber://chat?number=+PHONE_WITH_PLUS" class="d-none d-lg-block">Text to Viber (Desktop)</a>