swift_notifications 1.0.2
swift_notifications: ^1.0.2 copied to clipboard
A unified Flutter plugin for rich push and local notifications on Android, iOS, and macOS with no native code required.
Swift Notifications #
The easiest way to add notifications to your Flutter app!
Show notifications with images and buttons on Android, iOS, and macOS. No complicated setup needed!
🎯 What Can You Do? #
- ✅ Show simple text notifications
- ✅ Show notifications with images
- ✅ Add buttons to notifications (like Reply, Like, etc.)
- ✅ Send notifications from your server (Firebase)
- ✅ Open different screens when user taps notification
- ✅ Works when app is open, in background, or closed
📦 Install #
Add this line to your pubspec.yaml file:
dependencies:
swift_notifications: ^1.0.2
Then run:
flutter pub get
🚀 Quick Start (3 Steps) #
Step 1: Add This to Your main.dart #
import 'package:swift_notifications/swift_notifications.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize notifications
final notifications = SwiftNotifications();
await notifications.initialize();
await notifications.requestPermission();
runApp(MyApp());
}
Step 2: Show a Notification #
// Simple notification
await notifications.showSimpleNotification(
id: '1',
title: 'Hello!',
body: 'You have a new message',
);
// Notification with image
await notifications.showImageNotification(
id: '2',
title: 'New Photo',
body: 'Check this out!',
imageUrl: 'https://example.com/image.jpg',
);
// Notification with buttons
await notifications.showNotification(
NotificationRequest(
id: '3',
title: 'New Message',
body: 'You have a new message',
buttonsEnabled: true,
actions: [
NotificationAction(id: 'reply', title: 'Reply'),
NotificationAction(id: 'delete', title: 'Delete'),
],
),
);
Step 3: Handle When User Taps Notification #
notifications.onNotificationResponse.listen((response) {
print('User tapped notification: ${response.notificationId}');
// Navigate to a screen
Navigator.pushNamed(context, '/details');
});
That's it! You're done! 🎉
📱 Works On #
- ✅ Android
- ✅ iOS
- ✅ macOS
🔥 Send Notifications from Server (Firebase) #
Want to send notifications from your server? It's easy!
1. Add Firebase Files #
- Android: Put
google-services.jsoninandroid/app/folder - iOS: Put
GoogleService-Info.plistinios/Runner/folder
2. Enable Google Services (Android Only) #
In android/app/build.gradle.kts, add this line at the top:
plugins {
id("com.google.gms.google-services")
}
3. Send Notification from Firebase Console #
- Go to Firebase Console → Cloud Messaging
- Click "Send test message"
- Get your token:
await notifications.getFCMToken() - Paste token in Firebase Console
- Important: Use only "Custom data" section (don't fill title/text fields)
- Add these fields:
id: notification_123
title: New Message
body: You have a new message
image: https://example.com/image.jpg
imageEnabled: true
buttons: [{"id":"reply","title":"Reply"}]
buttonsEnabled: true
⚠️ Important: For images to work, use only Custom data. Don't fill "Notification title" and "Notification text" fields.
🧭 Open Different Screens from Notification #
Want to open a specific screen when user taps notification? Use screen_launch_by_notfication:
import 'package:screen_launch_by_notfication/screen_launch_by_notfication.dart';
SwiftFlutterMaterial(
materialApp: MaterialApp(
routes: {
'/': (context) => HomeScreen(),
'/message': (context) => MessageScreen(),
},
),
onNotificationLaunch: ({required isFromNotification, required payload}) {
if (isFromNotification && payload['type'] == 'message') {
return SwiftRouting(
route: '/message',
payload: payload,
);
}
return null;
},
)
📚 Common Tasks #
Show Simple Notification #
await notifications.showSimpleNotification(
id: '1',
title: 'Hello',
body: 'This is a notification',
);
Show Notification with Image #
await notifications.showImageNotification(
id: '2',
title: 'New Photo',
body: 'Check this out!',
imageUrl: 'https://example.com/image.jpg',
);
Show Notification with Buttons #
await notifications.showNotification(
NotificationRequest(
id: '3',
title: 'New Message',
body: 'You have a new message',
buttonsEnabled: true,
actions: [
NotificationAction(id: 'reply', title: 'Reply'),
NotificationAction(id: 'delete', title: 'Delete'),
],
),
);
Listen to Notification Taps #
notifications.onNotificationResponse.listen((response) {
print('Tapped: ${response.notificationId}');
print('Action: ${response.actionId}');
});
Get Firebase Token #
final token = await notifications.getFCMToken();
print('Token: $token');
Cancel Notification #
await notifications.cancelNotification('notification_id');
🐛 Common Problems #
Images Not Showing #
Problem: Images don't show when app is closed.
Solution: In Firebase Console, use only Custom data. Don't fill "Notification title" and "Notification text" fields.
Permission Denied #
Solution: Request permission:
await notifications.requestPermission();
App Not Opening on Tap #
Solution: Already fixed! The plugin automatically opens the app.
💡 Tips #
- Always request permission before showing notifications
- Use unique IDs for each notification
- For Firebase: Use only Custom data for rich notifications
- Test on real device - notifications don't work well on emulator
📖 More Examples #
Check the example/ folder for a complete working app.
🤝 Need Help? #
- Check the example app
- Read the code comments
- Open an issue on GitHub
📄 License #
MIT License - feel free to use in your projects!
Made with ❤️ for Flutter developers