我的 Ionic 应用程序在使用 OneSignal 时抛出 "has no method 'startInit'" 错误

My Ionic app throws a "has no method 'startInit'" error when using OneSignal

在我的 Ionic smart phone 应用程序中,我使用 OneSignal 来处理推送通知。根据一些 OneSignal 文档,我需要像这样初始化他们的插件:

window.plugins.OneSignal.init(
    "b2f7f966-d8cc-11e4-bed1-df8f05be55ba", 
    {googleProjectNumber: "703322744261"}, 
    notificationOpenedCallback
);

这对我有用。但是其他文档说我应该这样做:

window.plugins.OneSignal
    .startInit("YOUR_APPID", "YOUR_GOOGLE_PROJECT_NUMBER_IF_ANDROID")
    .handleNotificationReceived(function(jsonData) {
        alert("Notification received:\n" + JSON.stringify(jsonData));
        console.log('Did I receive a notification: ' + JSON.stringify(jsonData));
    })
    .endInit();

这是我更愿意做的,因为我真的想要 "handleNotificationReceived" 选项,而不仅仅是 "notificationOpenedCallback" 选项。但这对我不起作用。我收到一个 Javascript 错误,指出 "startInit" 方法在 "window.plugins.OneSignal" 对象中不存在。这是正确的。没有方法"startInit",那我怎么用调用它的代码呢?

那我做错了什么?我使用的不是最新版本的 OneSignal 吗?我只是 运行 "ionic plugin add onesignal-cordova-plugin --save",所以我应该使用他们的最新版本。 (根据我的 config.xml,它安装版本 ~1.13.2)。

我该如何解决这个问题?

较新的 2.+ 版本的 SDK 尚未发布。你应该参考旧文档 here

编辑

新版SDK已出,大家可以获取here. Also new docs are here.

遇到同样的问题。您可以尝试重新安装插件或重新添加平台 android/ios。这对我有用。