离子本地通知TypeError:Object(...)不是函数

时间:2019-01-25 14:13:26

标签: cordova ionic-framework ionic-native localnotification

我在localNotifications插件上调用了schedule方法后,出现了此错误。

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {SocketsProvider} from "../../providers/sockets/sockets";
import {LocalNotifications} from "@ionic-native/local-notifications/ngx";

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

constructor(public navCtrl: NavController,
            private socket: SocketsProvider,
            private localNotifications: LocalNotifications) {
}

ionViewDidLoad() {
    this.socket.get().listen('ReservationGuideAssigned', notification => {
        console.log(notification);
        this.data.incrementNotificationsCount(1);
        this.launchNotification(notification.reservation_id, notification.reservation_code);
    });
}

private launchNotification(reservationId, reservationCode) {
    this.localNotifications.schedule({
        title: 'Nouvelle reservation',
        text: `Réservation No ${reservationCode}`,
        data: {
            reservationId: reservationId,
        }
    });
}
}

app.module.ts

// ....

import {LocalNotifications} from "@ionic-native/local-notifications/ngx";

@NgModule({
    // ....
    providers: [
        LocalNotifications,
    ]
})
export class AppModule {
}

package.json

{
//....
"dependencies": {
"@angular/animations": "5.2.11",
"@angular/common": "5.2.11",
"@angular/compiler": "5.2.11",
"@angular/compiler-cli": "5.2.11",
"@angular/core": "5.2.11",
"@angular/forms": "5.2.11",
"@angular/http": "5.2.11",
"@angular/platform-browser": "5.2.11",
"@angular/platform-browser-dynamic": "5.2.11",
"@ionic-native/core": "~4.18.0",
"@ionic-native/local-notifications": "^5.0.0",
"@ionic-native/splash-screen": "~4.18.0",
"@ionic-native/status-bar": "~4.18.0",
"@ionic/storage": "^2.2.0",
"cordova-android": "^7.1.4",
"cordova-plugin-device": "^2.0.2",
"cordova-plugin-ionic-keyboard": "^2.1.3",
"cordova-plugin-ionic-webview": "^2.3.1",
"cordova-plugin-splashscreen": "^5.0.2",
"cordova-plugin-statusbar": "^2.4.2",
"cordova-plugin-whitelist": "^1.3.3",
"ionic-angular": "3.9.2",
"ionicons": "3.0.0",
"laravel-echo": "^1.5.2",
"pusher-js": "^4.3.1",
"rxjs": "5.5.11",
"sw-toolbox": "3.6.0",
"zone.js": "0.8.26"
},
"devDependencies": {
  "@ionic/app-scripts": "3.2.1",
  "typescript": "~2.6.2"
},
"description": "An Ionic project",
"cordova": {
"plugins": {
  "cordova-plugin-whitelist": {},
  "cordova-plugin-statusbar": {},
  "cordova-plugin-device": {},
  "cordova-plugin-splashscreen": {},
  "cordova-plugin-ionic-webview": {
    "ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
  },
  "cordova-plugin-ionic-keyboard": {},
  "cordova-plugin-local-notification": {}
},
"platforms": [
  "android"
]
}
}

config.xml

<plugin name="cordova-plugin-whitelist" spec="1.3.3" />
<plugin name="cordova-plugin-statusbar" spec="2.4.2" />
<plugin name="cordova-plugin-device" spec="2.0.2" />
<plugin name="cordova-plugin-splashscreen" spec="5.0.2" />
<plugin name="cordova-plugin-ionic-webview" spec="^2.0.0" />
<plugin name="cordova-plugin-ionic-keyboard" spec="^2.0.5" />
<engine name="android" spec="^7.1.4" />
<plugin name="cordova-plugin-local-notification" spec="~0.9.0-beta.3" />

这是错误

Uncaught TypeError: Object(...) is not a function
at LocalNotifications.schedule (index.js:49)
at HomePage.webpackJsonp.76.HomePage.launchNotification (home.ts:76)
at home.ts:49
at PrivateChannel.Dispatcher.emit (pusher.js:1596)
at PrivateChannel.Channel.handleEvent (pusher.js:3238)
at pusher.js:136
at ConnectionManager.Dispatcher.emit (pusher.js:1596)
at message (pusher.js:8168)
at Connection.Dispatcher.emit (pusher.js:1596)
at message (pusher.js:2984)

我尝试更新@ ionic-native,但遇到相同的错误,但是在应用程序启动时。

我还尝试使用如下代码直接与cordova合作:

cordova.plugins.notification.local.schedule(...)

但是cordova在浏览器中未知,因此应用程序崩溃。

请帮助我,我需要使用离子本机,以便我可以测试应用程序的其他组件而不会被cordova所阻止。

谢谢

1 个答案:

答案 0 :(得分:0)

很遗憾,您不能在浏览器上使用@ionic-native

import {LocalNotifications} from "@ionic-native/local-notifications/ngx";

您只能在真实设备或模拟器上运行它。