如何在我的应用程序中显示本地通知?

How to display a local notification in my app?

我的代码如下所示:

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

@Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
})
export class HomePage {
    framework = 'At exact boarding time';

    constructor(public platform: Platform) {}

    ionViewDidEnter() {
        this.platform.ready().then(() => {
            single_notification()
        });
    }
}

function single_notification() {
    cordova.plugins.notification.local.schedule({
      id: 1,
      text: 'Single ILocalNotification',
      sound: 'file://sound.mp3',
      data: { secret: 'key_data' }
    });
}

我收到此错误:Property 'notification' does not exist on type 'CordovaPlugins'

我希望代码位于主页之外 class。我在主页 class 中创建了它,没有出现任何错误,但它不会显示在我的设备上。我只想能够从主页外部调用函数 single_notification() 并显示通知。


编辑:使用 Emad Abdou 回答时出现这些错误:

首先,请确保您已在 app.module.ts 提供商中导入 LocalNotifications 然后在你的代码中试试这个

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

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
 framework = 'At exact boarding time';

 constructor(
      public platform: Platform,
      private localNotifications: LocalNotifications) {}

ionViewDidEnter() {
    this.platform.ready().then(() => {
        this.single_notification()
    });
  }
}

single_notification() {
  this.localNotifications.schedule({
   id: 1,
   text: 'Single ILocalNotification',
   sound: 'file://sound.mp3',
   data: { secret: 'key_data' }
 });
}

查看文档以获取更多信息 https://ionicframework.com/docs/native/local-notifications