如何用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
相关产品推荐
相关产品推荐

