javascript - 如何在我的应用中显示本地通知?
问题描述
我的代码如下所示:
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'
我想让代码在 Homepage 类之外。我从主页类中创建它,它没有给出错误,但它不会显示在我的设备上。我只想能够从主页外部调用函数 single_notification() 并显示通知。
解决方案
首先,确保您已在 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
推荐阅读
- python - 在python中将实例类型更改为继承
- python - 将数据框转换为字典
- python-3.x - Python3 - 将 unicode 文字字符串转换为 unicode 字符串
- python - 如何在需要时避免“if”嵌套python中的多个返回值
- spring-boot - 授权-Spring boot:RestTemplate + ApacheClient + JWT
- ajax - 我无法在 AJAX 使用 laravel 恢复的两个表中显示数据表中的数据
- c - 为什么即使我将其作为参数提供,gcc 也无法识别我的 .c 文件?
- javascript - 我不明白为什么这个函数的输出是一
- c# - 如何端到端测试这个 .net 核心控制台应用程序?
- docker - 为什么我们在容器内的工作不应该修改容器本身的内容呢?