Ionic中用cordova-plugin-local-notification实现每日定点本地通知及问题排查
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
LocalNotificationsservice: Ditch the directcordova.pluginscall 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: 1to give the system a clear target - Set
clearable: trueto enable Android’s swipe-to-delete - Explicitly set
ongoing: falseto avoid persistent alerts
Extra Checks:
- Update your plugin: Run
cordova plugin update cordova-plugin-local-notificationsto 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

