uni-app 个别获取 clientid 为null

uni-app 个别获取 clientid 为null

开发环境 版本号 项目创建方式
Mac 10.15.6 HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Mac

PC开发环境操作系统版本号:10.15.6

HBuilderX类型:正式

HBuilderX版本号:3.2.9

手机系统:Android

手机系统版本号:Android 11

手机厂商:ROG Phone 3

手机机型:ROG Phone 3

页面类型:vue

打包方式:云端

项目创建方式:HBuilderX

### 示例代码:

```javascript
setTimeout(() => {    
    const cid_res = plus.push.getClientInfo();    
    console.log(cid_res);    
},2000)    

setTimeout(() => {    
    console.log('aaaaaa');    
    plus.push.getClientInfoAsync( result => {    
       console.log(result);    
    }, (error)=>{    
       console.log(error);    
    });    
},5000)

操作步骤:

app.vue中

setTimeout(() => {
    const cid_res = plus.push.getClientInfo();
    console.log(cid_res);
},2000)

setTimeout(() => {
    console.log('aaaaaa');    
    plus.push.getClientInfoAsync( result => {    
       console.log(result);    
    }, (error)=>{    
       console.log(error);    
    });    
},5000)

预期结果:

{
    "id": "unipush",
    "token": "**",
    "clientid": "**",
    "appid": "uhFZGNm9CU8HBJpP6B4BS",
    "appkey": "mogSSeOQAF98DLpXlNnFi6"
}

实际结果:

{
    "id": "unipush",
    "token": "null",
    "clientid": "null",
    "appid": "uhFZGNm9CU8HBJpP6B4BS",
    "appkey": "mogSSeOQAF98DLpXlNnFi6"
}

{
    "code": -1,
    "message": "failed,check appkey or appid"
}

bug描述:

获取clientid为null


更多关于uni-app 个别获取 clientid 为null的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 个别获取 clientid 为null的实战教程也可以访问 https://www.itying.com/category-93-b0.html


根据你的描述,这是一个典型的 UniPush 初始化问题。获取 clientid 为 null 通常是因为推送服务尚未完成初始化。

核心原因分析

  1. 初始化时机问题:在 app.vueonLaunch 中直接调用 plus.push.getClientInfo() 时,推送服务可能还未完全初始化完成。
  2. 异步获取失败getClientInfoAsync 返回错误码 -1,表明推送服务初始化失败或配置有问题。

解决方案

方案一:使用正确的生命周期钩子(推荐)

app.vue 中使用 onShow 而不是 onLaunch,并添加适当的延迟:

export default {
  onShow: function() {
    // 延迟获取,确保推送服务已初始化
    setTimeout(() => {
      this.getClientInfo();
    }, 1000);
  },
  methods: {
    getClientInfo() {
      const info = plus.push.getClientInfo();
      if (info && info.clientid && info.clientid !== 'null') {
        console.log('获取到clientid:', info.clientid);
        // 处理获取到的clientid
      } else {
        // 如果未获取到,尝试异步获取
        plus.push.getClientInfoAsync(
          (result) => {
            console.log('异步获取成功:', result);
          },
          (error) => {
            console.log('异步获取失败:', error);
            // 可以在这里设置重试机制
          }
        );
      }
    }
  }
}

方案二:监听推送服务就绪事件

// 在 app.vue 的 onLaunch 中
onLaunch: function() {
  // 监听推送服务就绪事件
  document.addEventListener('plusready', () => {
    setTimeout(() => {
      const info = plus.push.getClientInfo();
      console.log('推送服务就绪后获取:', info);
    }, 500);
  });
}
回到顶部