离子本地通知 TypeError: Object(...) is not a function

Posted

技术标签:

【中文标题】离子本地通知 TypeError: Object(...) is not a function【英文标题】:ionic local notifications TypeError: Object(...) is not a function 【发布时间】:2019-01-25 14:13:25 【问题描述】:

我在 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在浏览器中是未知的,所以应用程序崩溃了。

请帮助我,我需要使用 ionic native,这样我就可以测试应用程序的其他组件而不会被 cordova 阻止。

谢谢

【问题讨论】:

这是因为 Ionic 已经更新了所有插件,所以我建议使用该插件的低版本。!如果您当前的版本是 4.5.0 或其他版本,那么您必须将其降级为 4.3.2 或其他版本。 【参考方案1】:

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

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

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

【讨论】:

以上是关于离子本地通知 TypeError: Object(...) is not a function的主要内容,如果未能解决你的问题,请参考以下文章

应用程序关闭时,离子 2 本地通知不起作用

在离子本地通知中访问构造函数内部的变量

使用cordova本地通知和离子设置重复间隔的特定数据和时间

date.getTime 不是函数离子本地通知错误

附件属性在离子本地通知插件中不起作用

无法在点击事件时设置离子本地通知(奇怪的语法错误)