【问题标题】:Send Ionic 3 Local Notification every day at a specific time每天在特定时间发送 Ionic 3 本地通知
【发布时间】:2018-09-26 09:47:58
【问题描述】:

我已使用以下命令将 Ionic 3 本地通知插件添加到我的项目中:

ionic cordova plugin add cordova-plugin-local-notification
npm install --save @ionic-native/local-notifications

我在构造函数上添加了所有依赖项。

我的代码是:

let year = new Date().getFullYear();
let month = new Date().getMonth();
let day = new Date().getDate();

let time1 = new Date(year, month, day, 10, 00, 0, 0);
let time2 = new Date(year, month, day, 12, 00, 0, 0);

this.localNotifications.schedule([
  {
    id: 1,
    title: 'My first notification',
    text: 'First notification test one',
    trigger: { at: new Date(time1) },
    data: {"id": 1, "name": "Mr. A"}
  },
  {
    id: 2,
    title: 'My Second notification',
    text: 'Second notification on 12 pm',
    trigger: { at: new Date(time2) },
    data: {"id": 2, "name": "Mr. B"}
  }
]);

它适用于当天应用启动,但我想每天在指定时间发送通知。

我特别想要本地通知,而不是推送通知。

【问题讨论】:

    标签: angular cordova typescript ionic-framework ionic3


    【解决方案1】:

    为了每天重复通知,您需要使用every:"day"(或以分钟为单位的间隔:every: 24*60)和firstAt 属性与第一次触发通知的日期。试试这个代码

    let year = new Date().getFullYear();
    let month = new Date().getMonth();
    let day = new Date().getDate();
    
    let time1 = new Date(year, month, day, 10, 00, 0, 0);
    let time2 = new Date(year, month, day, 12, 00, 0, 0);
    
    this.localNotifications.schedule([
      {
        id: 1,
        title: 'My first notification',
        text: 'First notification test one',
        firstAt: new Date(time1),
        every: 24*60,
        data: {"id": 1, "name": "Mr. A"}
      },
      {
        id: 2,
        title: 'My Second notification',
        text: 'Second notification on 12 pm',
        firstAt: new Date(time2),
        every: 24*60,
        data: {"id": 2, "name": "Mr. B"}
      }
    ]);
    

    【讨论】:

    • 不工作。它显示类型脚本错误。 firstAt 不存在于类型 ILocalNotification | ILocalNotification[]
    • 奇怪,因为它存在于文档github.com/katzer/cordova-plugin-local-notifications
    • 我也奇怪。不知道为什么这在我的代码中不起作用。我找到了一个对我有用的示例代码。 cordova.plugins.notification.local.schedule({ title: 'Happy Birthday!!!', trigger: { every: { month: 4, day: 24, hour: 9, minute: 0 } } });
    • 但这不会每天运行吗?
    • @AmanullahAman 如果我删除月份和日期,通知将无限循环
    【解决方案2】:

    在他们的代码库中显示(注释)您可以通过这样做来实现这一目标

    this.localNotifications.schedule({
     text: 'Delayed ILocalNotification',
     trigger: {at: new Date(new Date().getTime() + 3600)},
     led: 'FF0000',
     sound: null});
    

    现在,如果您必须每天在同一时间发送通知,您可以:

    1 - 安排十分之一的通知并在用户每次打开您的应用时检查

    2 - 每次用户打开已收到的通知时重新安排通知。

    【讨论】:

      【解决方案3】:

      为了每天重复通知,您需要使用every:"day"firstAt 属性以及第一次触发通知的日期。

      注意:与 Ionic 3 中的 cordova 插件不同,firstAt 属性需要包装在 trigger 属性中。您可以在Ionic Local Notification Documentation找到更多信息。

      试试这个代码

      let year = new Date().getFullYear();
      let month = new Date().getMonth();
      let day = new Date().getDate();
      
      let time1 = new Date(year, month, day, 10, 00, 0, 0);
      let time2 = new Date(year, month, day, 12, 00, 0, 0);
      
      this.localNotifications.schedule([
        {
          id: 1,
          title: 'My first notification',
          text: 'First notification test one',
          trigger: {firstAt: new Date(time1)},
          every: every: "day"
          data: {"id": 1, "name": "Mr. A"}
        },
        {
          id: 2,
          title: 'My Second notification',
          text: 'Second notification on 12 pm',
          trigger: {firstAt: new Date(time2)}, 
          every: "day",  //"day","hour","minute","week" can be used
          data: {"id": 2, "name": "Mr. B"}
        }
      ]);
      

      【讨论】:

        【解决方案4】:

        所以我自己找到了解决方案。这是 LocalNotifications 包的类型文件中的错误。

        选项的正确用法如下所示:

            {
              id: 1,
              title: 'Notification Title',
              text: 'Your notification text',
              foreground: true,
              trigger: {
                every: {
                  hour: 8,
                  minute: 15
                }
              }
            }
        

        只需进入您的 node_modules/@ionic-native/local-notifications 找到 index.d.ts 并找到显示 every?: ELocalNotificationTriggerUnit 的行并将其更改为 every?: any;现在它应该可以正常工作了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-01-09
          • 1970-01-01
          • 1970-01-01
          • 2023-01-24
          • 2021-07-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多