angular - ionic 电容器本地通知

标签 angular typescript ionic-framework capacitor

我正在尝试实现 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/

相关文章:

angular - 带过滤器的 ngrx 选择器

typescript 引用错误 : exports is not defined

TypeScript 需要一个参数或另一个参数,但两者都不需要

angular - Ionic5 应用程序与 SupaBase 的连接给出 "process is not defined"

ios - 无法从 Parse 服务器和 ionic 应用程序 (ios) 接收推送通知

javascript - 为什么我的 browserify 任务会在导出的末尾附加两次

angular - Angular 2 的数据透视表组件?

javascript - 如何在 Angular 5 组件中有条件地加载 templateUrl html 文件

Angular Material Snackbar 未正确显示

cordova - 在 Ionic 2 中,如何在键盘显示时将元素 float 到键盘上方?