You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Appium/Wdio在iOS模拟器中获取推送通知文本?

iOS模拟器中用Appium/Wdio JS获取推送通知文本的解决方案

核心方法:使用mobile: getNotification命令

iOS端没有Android的openNotifications()方法,但可以通过Appium专属的mobile: getNotification命令直接获取通知内容,替代你之前无效的expectNotification调用。

可运行的实现代码

// 先等待通知出现(避免因推送延迟导致获取失败)
await driver.waitUntil(async () => {
  const notifications = await driver.execute('mobile: getNotification', {});
  return notifications.length > 0;
}, { timeout: 10000, timeoutMsg: '10秒内未收到目标通知' });

// 获取所有通知并筛选目标项
const notifications = await driver.execute('mobile: getNotification', {});
const targetNotify = notifications.find(item => item.title === 'AutomationTest');

// 提取并输出通知文本
if (targetNotify) {
  console.log('目标通知内容:', targetNotify.body);
} else {
  console.log('未找到标题为AutomationTest的通知');
}

关键细节说明

  • mobile: getNotification默认返回当前模拟器所有可见推送通知的数组,每个通知对象包含title、body、subtitle等字段
  • 必须添加等待逻辑:推送通知存在延迟,直接调用可能返回空数组
  • 可根据实际需求调整筛选条件,比如通过subtitle或body片段匹配通知

原代码无效的原因

你使用的mobile: expectNotification主要用于断言通知是否存在,且部分Appium版本对该命令的支持不稳定,而mobile: getNotification是官方推荐的、更可靠的通知内容获取方式。

内容的提问来源于stack exchange,提问作者awhisler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 23:47:01