You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic中用cordova-plugin-local-notification实现每日定点本地通知及问题排查

Solution for Daily 10 AM Notifications & Swipe-to-Delete Issues

Hey Aaron, let's fix your two problems with local notifications in your Ionic app. I'll break this down into clear, actionable steps for both issues:

1. Fixing Daily 10 AM Scheduled Notifications

Your current code has a small oversight: if the app launches after 10 AM, the firstAt time will be in the past, triggering the notification immediately instead of waiting until the next day. Here's how to adjust that, plus add better notification management:

Key Improvements:

  • Adjust first notification time based on current hour: If it's already past 10 AM, schedule the first alert for tomorrow instead of today.
  • Add a unique notification ID: This helps the system track and manage notifications (critical for fixing the swipe-to-delete issue too).
  • Use the injected LocalNotifications service: Ditch the direct cordova.plugins call for the Angular-provided service—it’s more consistent with Ionic’s structure.

Modified pushNotification() Method:

pushNotification() {
  const now = moment();
  let pushTime = moment().hours(10).minutes(0).seconds(0).milliseconds(0);

  // Shift to tomorrow if current time is already past 10 AM
  if (now.isAfter(pushTime)) {
    pushTime.add(1, 'day');
  }

  const notificationConfig = {
    id: 1, // Unique ID for this daily notification
    text: 'Aaron Harper Notification',
    every: 'day',
    firstAt: pushTime.toDate(),
    clearable: true, // Explicitly allow clearing (fixes swipe delete)
    ongoing: false // Ensure notification isn't marked as persistent
  };

  // Use the injected service instead of raw cordova global
  this.localNotifications.schedule(notificationConfig);
}

2. Fixing Swipe-to-Delete for Notifications

Notifications that can’t be swiped away almost always boil down to one of these issues:

  • No unique id (the system can’t identify which notification to delete)
  • ongoing: true (persistent notifications are locked in the shade)
  • Missing clearable: true (Android requires this flag for swipe-to-delete)

In the updated code above, we addressed all three:

  • Added id: 1 to give the system a clear target
  • Set clearable: true to enable Android’s swipe-to-delete
  • Explicitly set ongoing: false to avoid persistent alerts

Extra Checks:

  • Update your plugin: Run cordova plugin update cordova-plugin-local-notifications to ensure you’re on the latest version (old versions have bugs with notification clearing).
  • Test on physical devices: Emulators often have inconsistent notification behavior—always validate on real iOS/Android hardware.

Bonus: Prevent Duplicate Notifications

To avoid scheduling the same 10 AM alert multiple times (e.g., when the app restarts), add a quick check before scheduling:

async pushNotification() {
  const existingNotifications = await this.localNotifications.getAll();
  const hasDailyAlert = existingNotifications.some(n => n.id === 1);

  if (!hasDailyAlert) {
    // ... existing scheduling code here ...
  }
}

This ensures you only have one daily 10 AM notification active at a time.

内容的提问来源于stack exchange,提问作者aaron harper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:01:51