Search code examples
ionic-frameworknotificationsionic3localnotification

ionic local notification error: Uncaught (in promise): TypeError: Object(...) is not a function


I want to add local notification in my ionic project.I added in app.module.ts;

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

and I added LocalNotifications in providers.

also in home.ts I write this codes;

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
});

I also did the definition of import and constructor in home.ts.I get the following error when I run my code on android device;

**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)**

Can you help me?


Solution

  • if you are using ionic 3 visit this link

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

    install cordova plugin & npm

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

    need to import in app.module.ts file

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

    Add LocalNotifications in providers array [app.module.ts file]

     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(),
                });
            });
    

    set sound by placing .mp3 sound file at src/assets/sounds/sound.mp3

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

    you can read the hidden message after notification received with subscribe method (can add in home page or app.component.ts file)

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