离子电容器本地通知



我正在尝试实现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调试中,我得到了这样的结果:

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

这适用于电容器3

官方文档: https://capacitorjs.com/docs/apis/local-notifications#install

本地通知.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,
},
},
],
});
}
}

潜在客户详细信息.组件.ts

createReminder(lead: LeadModel): void {
this.localNotificationsService.showLocalNotification(
lead.name,
lead.address,
new Date(lead.reminderDate)
);
}

lead.model.ts

export interface LeadModel {
name?: string;
address?: string;
reminderDate?: string;
}

相关内容

  • 没有找到相关文章

最新更新