我正在尝试实现 Capacitor LocalNotification 插件。
import { Plugins, CameraResultType } from '@capacitor/core';
const { LocalNotifications } = Plugins;
在下面的代码中点赞
schedule() {
console.log('----')
LocalNotifications.schedule({
notifications: [
{
title: "aaaa",
body: "Body",
id: 1,
smallIcon: 'house',
actionTypeId: 'OPEN_PRODUCT',
schedule: {
every: "minute"
},
extra: null
}
]
});
}
我在 ios 模拟器上测试,方法被调用,在 Xcode-debug 中我得到这样的结果:
To Native -> LocalNotifications schedule 1850642
⚡️ TO JS {"notifications":[{"id":"1"}]
但是模拟器中没有显示通知。
我没有在这个项目中使用 cordova/ionic-native。如果我需要运行一些 npm 包等来让它工作,请帮忙。
最佳答案
它应该可以在模拟器上运行,所以这不是问题所在。我认为问题在于 every
参数与 repeats
一起使用,您还需要指定何时显示第一个通知。
export interface LocalNotificationSchedule {
at?: Date;
repeats?: boolean;
every?: 'year' | 'month' | 'two-weeks' | 'week' | 'day' | 'hour' | 'minute' | 'second';
count?: number;
on?: {
year?: number;
month?: number;
day?: number;
hour?: number;
minute?: number;
};
}
因此,例如,如果您希望将通知设置为在函数调用后一分钟(以及之后的每一分钟)显示,可以这样做:
schedule() {
const randomId = Math.floor(Math.random() * 10000) + 1;
LocalNotifications.schedule({
notifications: [
{
title: "Test Title",
body: "Test Body",
id: randomId,
schedule: {
at: new Date(Date.now() + 1000 * 60) // in a minute
repeats: true,
every: "minute"
}
}
]
});
}
关于angular - ionic 电容器本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65202776/