【问题标题】:Ionic capacitor Local Notification离子电容器本地通知
【发布时间】:2021-03-20 00:02:45
【问题描述】:

我正在尝试实现 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 包等以使其正常工作,请提供帮助。

【问题讨论】:

  • 您可以在真实设备上试用您的代码吗?

标签: angular typescript ionic-framework capacitor


【解决方案1】:

这适用于我的 Capacitor 3。

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

local-notifications.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

 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;
}

【讨论】:

    【解决方案2】:

    它应该可以在模拟器上运行,所以这不是问题。我认为问题在于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"
              }
            }
          ]
        });
      }
    

    【讨论】:

    • 我想你可以接受这个答案,不是吗? @raju
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多