升级OneSignal SDK至5.x后Android Kotlin项目出现未解析引用错误
OneSignal 5.x 升级后「Unresolved reference」编译错误修复方案
OneSignal 5.x 对SDK API做了大规模重构,旧4.x的核心方法、类名和包结构都有变更,以下是你遇到的具体问题的修复方案:
1. setAppId、setLogLevel 方法适配
旧4.x代码(编译报错):
OneSignal.setAppId("YOUR_APP_ID") OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE)
5.x替换代码:
// 必须先初始化上下文,5.x新增要求 OneSignal.initWithContext(applicationContext) OneSignal.setAppId("YOUR_APP_ID") // 日志级别枚举迁移到新包 import com.onesignal.core.internal.logging.LogLevel OneSignal.setLogLevel(LogLevel.VERBOSE, LogLevel.NONE)
2. OSNotificationOpenedHandler、OSNotificationOpenedResult 适配
旧4.x代码(MyNotificationOpenedHandler.kt 报错):
class MyNotificationOpenedHandler : OSNotificationOpenedHandler { override fun notificationOpened(result: OSNotificationOpenedResult) { // 旧处理逻辑 } } // 注册逻辑 OneSignal.setNotificationOpenedHandler(MyNotificationOpenedHandler())
5.x替换代码:
import com.onesignal.notifications.INotificationOpenedHandler import com.onesignal.notifications.NotificationOpenedEvent class MyNotificationOpenedHandler : INotificationOpenedHandler { override fun notificationOpened(event: NotificationOpenedEvent) { // 获取通知数据:event.notification.data // 获取用户点击动作:event.action // 原逻辑迁移到这里 } } // 注册方式变更 OneSignal.notifications.addNotificationOpenedHandler(MyNotificationOpenedHandler())
3. 额外排查步骤
- 检查所有OneSignal相关导入,旧的
com.onesignal.OS*类大多迁移到com.onesignal.notifications.*或com.onesignal.core.*包下 - 执行
File -> Invalidate Caches... -> Invalidate and Restart清理项目缓存,再重新同步Gradle
内容的提问来源于stack exchange,提问作者fcadevelopers
相关产品推荐
相关产品推荐

