uni-app oppo r9手机无法通过 plus.push.getClientInfo() 获取 clientid

uni-app oppo r9手机无法通过 plus.push.getClientInfo() 获取 clientid

开发环境 版本号 项目创建方式
HBuilderX 3.1.22 云端

示例代码:

APP.VUE页面
···
onLaunch: function() {
console.log('App Launch');
// #ifdef APP-PLUS
let result = plus.push.getClientInfo();
if (result && result.clientid && result.clientid !== 'null') {
console.info('初始化客户端标识={APP}', result.clientid);
uni.setStorageSync('deviceNo', result.clientid);
} else {
var obtainingCIDTimer = setInterval(function() {
result = plus.push.getClientInfo();
if (result.clientid && result.clientid !== 'null') {
console.info('延迟获取客户端标识={APP}', result.clientid);
uni.setStorageSync('deviceNo', result.clientid);
clearInterval(obtainingCIDTimer);
}else{
console.error('5+ API首次初始化客户端标识 clientid 失败,原因:客户端向推送服务器注册还未完成,正在延时重试。');
}
}, 50);
}
// #endif
},
···

操作步骤:

let cid= plus.push.getClientInfo().clientid;
console.log(cid)

预期结果:

正常或延时获取到cid非null值

实际结果:

{
"id": "unipush",
"token": "null",
"clientid": "null",
"appid": "pPyZWvH3Fa6PXba10aJ009",
"appkey": "b7dOGlNPHR7pqwUxDhpTi4"
}

更多关于uni-app oppo r9手机无法通过 plus.push.getClientInfo() 获取 clientid的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app oppo r9手机无法通过 plus.push.getClientInfo() 获取 clientid的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在OPPO R9等部分Android设备上,plus.push.getClientInfo()返回clientid为null是常见问题,主要涉及设备兼容性和推送服务初始化时机。

问题分析:

  1. 推送服务初始化延迟 - 部分OPPO设备系统对推送服务的启动管理较严格,导致应用启动时推送服务尚未就绪
  2. 设备兼容性问题 - 较老的OPPO机型可能存在与uni-app推送模块的兼容性差异
  3. 网络环境因素 - 设备首次注册推送服务需要网络连接,网络不稳定会导致注册失败

解决方案:

  1. 增加重试机制和超时处理
onLaunch: function() {
  // #ifdef APP-PLUS
  let retryCount = 0;
  const maxRetry = 100; // 最大重试次数
  const obtainingCIDTimer = setInterval(() => {
    let result = plus.push.getClientInfo();
    if (result && result.clientid && result.clientid !== 'null') {
      console.info('获取客户端标识成功:', result.clientid);
      uni.setStorageSync('deviceNo', result.clientid);
      clearInterval(obtainingCIDTimer);
    } else {
      retryCount++;
      if(retryCount >= maxRetry) {
        console.error('获取clientid超时');
        clearInterval(obtainingCIDTimer);
        // 可考虑降级方案,如使用设备唯一标识
        this.getFallbackDeviceId();
      }
    }
  }, 100); // 间隔调整为100ms
  // #endif
}
  1. 添加网络状态检查 在尝试获取clientid前检查网络状态:
plus.networkinfo.getCurrentType((type) => {
  if(type === plus.networkinfo.CONNECTION_NONE) {
    console.error('网络未连接,无法获取clientid');
    return;
  }
  // 执行获取clientid逻辑
});
  1. 使用备用设备标识 当无法获取clientid时,可使用设备唯一标识作为降级方案:
getFallbackDeviceId() {
  plus.device.getInfo({
    success: function(device) {
      let deviceId = device.uuid || device.imei || `${Date.now()}_${Math.random()}`;
      uni.setStorageSync('deviceNo', deviceId);
    }
  });
}
回到顶部