576501565
576501565
  • 发布:2026-08-21 10:32
  • 更新:2026-08-21 10:33
  • 阅读:17

webview如何拿到第三方推送的token

分类:uni-app

<web-view :src="url" ></web-view>

webview加载了h5页面,h5页面内如何得到第三方推送的token

2026-08-21 10:32 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

欢迎到专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。

在 app 端,<web-view> 加载的 h5 页面中是带有 5+(plus)运行环境的,h5 页面在 plusready 之后即可直接调用推送相关 api 获取 token,无需 app 端中转。参考 web-view 组件 faq 中的说明:web-view 加载的 html 中是有 5+ 环境的,在 plusready 后调用即可。

方案一:h5 页面内直接获取(推荐)

在 h5 页面中监听 plusready,然后调用 plus.push.getClientInfo()

<script>  
document.addEventListener('plusready', function() {  
    var info = plus.push.getClientInfo();  
    console.log('clientid: ' + info.clientid); // 推送服务令牌(设备唯一标识)  
    console.log('token: ' + info.token);       // 设备令牌,ios 上为 devicetoken  
    console.log('appid: ' + info.appid);  
    console.log('appkey: ' + info.appkey);  
});  
</script>

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

返回字段含义可参考 uni.getprovider 兼容性说明 中 push 服务的定义:

  • clientid:推送服务令牌,用于标识推送信息接收者身份,第三方推送服务器管理的设备唯一标识
  • token:设备令牌,android 上通常与 clientid 一致,ios 上为 devicetoken 值,向 apns 发送推送时使用

注意事项

  1. 必须等 plusready 触发后再调用,否则 plus 对象不存在。如果 h5 里引入了 uni.webview.js,也可以监听 UniAppJSBridgeReady 事件,效果类似。
  2. app-nvue 页面中的 web-view 组件不能运行 plus api,此方案仅适用于 app-vue 页面的 web-view。
  3. 第三方推送(个推、小米推送等)需要在 manifest.json 中完成推送配置,并且需要自定义基座或打包后才能获取到,标准基座可能拿不到对应厂商的 token。
  4. 如果获取时机较早,推送 sdk 可能尚未注册完成,clientid 可能为空,可适当延时重试,或监听 plus.push.addEventListener('receive', ...) 确认推送服务已就绪。

方案二:h5 无法直接使用 plus 时,通过通信让 app 端传递

如果 h5 页面环境受限(例如 nvue 场景),可以在 app 端获取后注入给 h5:

app 端(vue 页面):

// #ifdef APP-PLUS  
const pages = getCurrentPages();  
const page = pages[pages.length - 1];  
const currentWebview = page.$getAppWebview();  
const wv = currentWebview.children()[0];  
const info = plus.push.getClientInfo();  
wv.evalJS(`window.receivePushToken && window.receivePushToken('${info.clientid}')`);  
// #endif

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

h5 页面:

<script>  
window.receivePushToken = function(token) {  
    console.log('收到推送token: ' + token);  
};  
</script>

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

注意 evaljs 需要在 web-view 加载完成后执行,页面初始化调用时建议适当延时。另外 web-view 的 @message 事件只在特定时机(后退、组件销毁、分享)触发,不适合实时传递,所以 app 向 h5 传值建议用 evaljs 方式。

内容为 AI 生成,仅供参考

要回复问题请先登录注册