Ионная локальная ошибка уведомления: Uncaught (в обещании): TypeError: Object (...) не является функцией - PullRequest
0 голосов
/ 15 марта 2019

Я хочу добавить локальное уведомление в мой ионный проект. Я добавил в app.module.ts;

import {LocalNotifications} из '@ ionic-native / local-notifications / ngx';

и я добавил LocalNotifications в провайдерах.

также в home.ts я пишу эти коды;

sendLocalNotifications() {

this.localNotifications.schedule({
  title: 'Local ILocalNotification Example',
  text: 'Delayed ILocalNotification',
  trigger: {at: new Date(new Date().getTime() + 3600)},
  led: 'FF0000',
  data: {secret: "asaddad"},

  sound: null
});

Я также сделал определение импорта и конструктора в home.ts. Я получаю следующую ошибку, когда запускаю свой код на устройстве Android;

**Error: Uncaught (in promise): TypeError: Object(...) is not a function
TypeError: Object(...) is not a function
    at LocalNotifications.schedule (vendor.js:92805)
    at HomePage.webpackJsonp.328.HomePage.sendLocalNotifications (main.js:2891)
    at main.js:2881
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (vendor.js:5134)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (vendor.js:5125)
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (vendor.js:5125)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)**

Вы можете мне помочь?

1 Ответ

1 голос
/ 16 марта 2019

если вы используете ionic 3, перейдите по этой ссылке

https://ionicframework.com/docs/v3/native/local-notifications/

установите плагин cordova & npm

$ ionic cordova plugin add cordova-plugin-local-notification
$ npm install --save @ionic-native/local-notifications@4

необходимо импортировать в app.module.tsfile

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

Добавить LocalNotifications в массиве провайдеров [файл app.module.ts]

 providers: [
            StatusBar,
            SplashScreen,
            ImagePicker,
            InAppBrowser,
            LoginService,
            ConnectivityService,
            Network,
            GooglePlus,
            GoogleServiceProvider,
            GoogleMapsKeyProvider,
            AppVersion,
            BarcodeScanner,
            Device,
            FCM,
            CheckStorageProvider,
            Facebook,
            Geolocation,
            TwitterConnect,
            LinkedIn,
            File,
            Camera,
            FileTransfer,
            FilePath,
            Base64,
            {provide: ErrorHandler, useClass: IonicErrorHandler},
            LocalNotifications
        ]


this.localNotifications.requestPermission().then((permission) => {
            this.localNotifications.schedule({
                id: 0,
                text: 'Delayed ILocalNotification',
                trigger: {at: date},
                foreground: true,
                vibrate: true,
                led: {color: '#FF00FF', on: 500, off: 500},
                data: {mydata: 'My hidden message this is'},
                sound: this.setSound(),
            });
        });

установить звук, поместив звуковой файл .mp3 в src / assets / sounds / sound.mp3

setSound() {
        if (this.platform.is('android')) {
            return 'file://assets/sounds/sound.mp3'
        } else {
            return 'file://assets/sounds/sound.mp3'
        }
    }

вы можете прочитать скрытое сообщение после получения уведомления методом подписки (можете добавить на домашнюю страницу или в файл app.component.ts)

if (_platform.is('cordova')) {
            this.localNotifications.on('click').subscribe((datas: any) => {
                    alert(JSON.stringify(datas));
                });
        }
...