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

Ionic最新更新后本地通知插件报错,求问题原因与解决方案

解决本地通知插件点击事件参数不匹配的问题

Hey there, I totally get how frustrating it is when updating dependencies breaks your code out of nowhere! Let's break down what's happening here and how to fix it.

问题根源

This error (Expected 1 arguments, but got 2.) almost certainly comes from a breaking change in the local notifications plugin (either cordova-plugin-local-notifications or its Ionic Native wrapper @ionic-native/local-notifications) after you updated your node_modules.

In older versions of the plugin, the click event callback accepted two separate arguments: the notification object and the state object. But in newer versions, the team likely refactored this to pass a single combined object that contains both the notification details and state information.

修复步骤

You just need to adjust your callback function to accept one parameter instead of two, then destructure or access the properties you need from that single object. Here's how to update your code:

原来的报错代码:

this.platform.ready().then(() => { 
  this.localNoti.on('click', (noti, state) => { 
    alert(state);
    // 其他业务逻辑
  });
});

修改后的正确代码:

this.platform.ready().then(() => { 
  this.localNoti.on('click', (eventData) => { 
    // 从组合对象中提取所需属性
    const noti = eventData.notification;
    const state = eventData.state;
    alert(state);
    // 其他业务逻辑
  });
});

额外排查建议

  • Double-check that the version of @ionic-native/local-notifications matches the version of the underlying cordova-plugin-local-notifications in your project. Mismatched versions often cause unexpected API inconsistencies.
  • Take a quick look at the plugin's release notes to confirm this breaking change—this will also help you catch any other API adjustments you might need to make elsewhere in your app.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:30:14