[英]Ionic capacitor Local Notification
I am trying to implement Capacitor LocalNotification plugin.我正在尝试实现 Capacitor LocalNotification 插件。
import { Plugins, CameraResultType } from '@capacitor/core';
const { LocalNotifications } = Plugins;
In the code below like like在下面的代码中
schedule() {
console.log('----')
LocalNotifications.schedule({
notifications: [
{
title: "aaaa",
body: "Body",
id: 1,
smallIcon: 'house',
actionTypeId: 'OPEN_PRODUCT',
schedule: {
every: "minute"
},
extra: null
}
]
});
}
I am testing on ios simulator, the method is getting called and in Xcode-debug i am getting like this:我正在 ios 模拟器上进行测试,该方法被调用,在 Xcode-debug 中我得到这样的结果:
To Native -> LocalNotifications schedule 1850642
⚡️ TO JS {"notifications":[{"id":"1"}]
But notification not showing in simulator.但通知未显示在模拟器中。
I am not using cordova/ionic-native in this project.我没有在这个项目中使用cordova/ionic-native。 Please help if i need to run some npm package etc to get this to work.
如果我需要运行一些 npm package 等以使其工作,请提供帮助。
It should work on the simulator so that's not the issue.它应该在模拟器上工作,所以这不是问题。 I think the problem is that the
every
parameter is used together with repeats
and you also need to specify when the first notification should be shown.我认为问题在于
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;
};
}
So for example if you want to set a notification to be shown one minute after the function is called (and every minute after that) it could be done like this:因此,例如,如果您想设置在调用 function 后一分钟(以及之后的每一分钟)显示通知,可以这样做:
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"
}
}
]
});
}
This works for me on Capacitor 3 .这适用于我在Capacitor 3上。
Offical doc: https://capacitorjs.com/docs/apis/local-notifications#install官方文档: https://capacitorjs.com/docs/apis/local-notifications#install
local-notifications.service.ts本地通知.service.ts
import { Injectable } from '@angular/core';
import { LocalNotifications } from '@capacitor/local-notifications';
import { random } from 'lodash';
@Injectable({
providedIn: 'root',
})
export class LocalNotificationsService {
constructor() {}
showLocalNotification(title: string, body: string, at: Date, id: number = random(0, 1000)): void {
LocalNotifications.schedule({
notifications: [
{
title,
body,
id,
schedule: {
at,
},
},
],
});
}
}
lead-details.component.ts潜在客户详细信息.component.ts
createReminder(lead: LeadModel): void {
this.localNotificationsService.showLocalNotification(
lead.name,
lead.address,
new Date(lead.reminderDate)
);
}
lead.model.ts铅.model.ts
export interface LeadModel {
name?: string;
address?: string;
reminderDate?: string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.