A Flutter plugin for iOS local notifications implemented in Objective-C using the native UserNotifications framework.
- β Request notification permissions
- β Schedule one-time notifications with custom delay
- β Schedule repeating notifications
- β Cancel individual notifications by ID
- β Cancel all notifications
- β Get list of pending notifications
- β Custom notification sounds
- β Foreground notification display
- β Notification tap handling
Platform | Support |
---|---|
iOS | β iOS 12.0+ |
Android | β Not implemented |
Add this to your package's pubspec.yaml
file:
dependencies:
kk_local_notifications_objc_plugin:
path: ../kk_local_notifications_objc_plugin
import 'package:kk_local_notifications_objc_plugin/kk_local_notifications_objc_plugin.dart';
final plugin = KKLocalNotificationsObjcPlugin();
// Request permissions first
bool granted = await plugin.requestNotificationPermissions();
if (granted) {
// Schedule a simple notification
await plugin.scheduleNotification(
title: 'Hello!',
body: 'This is a test notification',
delaySeconds: 5,
);
}
// Schedule notification with custom ID and sound
await plugin.scheduleNotification(
id: 'my-custom-id',
title: 'Important Reminder',
body: 'Don\'t forget your meeting at 3 PM',
delaySeconds: 10,
sound: 'default',
);
// Schedule repeating notification (minimum 60 seconds interval)
await plugin.scheduleRepeatingNotification(
id: 'daily-reminder',
title: 'Daily Reminder',
body: 'Time for your daily task!',
intervalSeconds: 3600, // 1 hour
);
// Cancel specific notification
await plugin.cancelNotification('my-custom-id');
// Get all pending notifications
List<Map<String, dynamic>> pending = await plugin.getPendingNotifications();
print('Pending notifications: ${pending.length}');
// Cancel all notifications
await plugin.cancelAllNotifications();
Request notification permissions from the user.
Future<bool> requestNotificationPermissions()
Returns: true
if permissions granted, false
otherwise.
Schedule a one-time notification.
Future<Map<String, dynamic>> scheduleNotification({
String? id,
required String title,
required String body,
int delaySeconds = 5,
String? sound,
})
Parameters:
id
: Optional custom ID. If not provided, a UUID will be generatedtitle
: Notification titlebody
: Notification body textdelaySeconds
: Delay before showing notification (default: 5)sound
: Sound name or 'default' (default: 'default')
Returns: Map with success
boolean and notification id
.
Schedule a repeating notification.
Future<Map<String, dynamic>> scheduleRepeatingNotification({
String? id,
required String title,
required String body,
int intervalSeconds = 60,
String? sound,
})
Parameters:
id
: Optional custom ID. If not provided, a UUID will be generatedtitle
: Notification titlebody
: Notification body textintervalSeconds
: Repeat interval in seconds (minimum: 60)sound
: Sound name or 'default' (default: 'default')
Returns: Map with success
boolean and notification id
.
Cancel a specific notification by ID.
Future<Map<String, dynamic>> cancelNotification(String id)
Parameters:
id
: The notification ID to cancel
Returns: Map with success
boolean and cancelledId
.
Cancel all scheduled and delivered notifications.
Future<Map<String, dynamic>> cancelAllNotifications()
Returns: Map with success
boolean and confirmation message
.
Get all pending (scheduled but not yet delivered) notifications.
Future<List<Map<String, dynamic>>> getPendingNotifications()
Returns: List of notification objects with id
, title
, body
, timeInterval
, and repeats
properties.
The plugin uses the iOS UserNotifications framework with these key components:
- Permission Handling: Requests alert, sound, and badge permissions
- Notification Scheduling: Uses
UNTimeIntervalNotificationTrigger
for timing - Delegate Implementation: Handles foreground display and tap events
- Content Management: Configures notification appearance and behavior
- Foreground Display: Notifications appear even when app is active
- Sound Support: Default system sound or custom sounds
- Badge Management: Automatic badge increment
- Error Handling: Comprehensive error reporting via Flutter method channels
- Thread Safety: All callbacks properly dispatched to main queue
The included example app demonstrates all functionality:
- Permission Request: Interactive permission handling
- Notification Scheduling: Forms for creating notifications
- Real-time Management: View and cancel pending notifications
- Status Updates: Live feedback on all operations
To run the example:
cd example
flutter run
- iOS 12.0 or later
- Xcode 12.0 or later
- Flutter 2.0 or later
Common error codes:
PERMISSION_ERROR
: Permission request failedSCHEDULE_ERROR
: Notification scheduling failedINVALID_INTERVAL
: Repeating notification interval too short (< 60s)MISSING_ID
: Required notification ID not provided
- Always request permissions first before scheduling notifications
- Use meaningful IDs for notifications you might need to cancel later
- Respect minimum intervals for repeating notifications (60 seconds)
- Test on physical devices - notifications don't work in simulators
- Handle permission denials gracefully in your app
- Clean up notifications when no longer needed
Run the included tests:
flutter test
The test suite covers all public API methods with mock implementations.
This plugin serves as an educational example of native iOS plugin development. Feel free to extend it with additional features like:
- Custom notification sounds
- Rich notifications with images
- Action buttons
- Location-based triggers
- Calendar-based scheduling