flutter_notification_service 1.0.0
flutter_notification_service: ^1.0.0 copied to clipboard
Collection of Widgets and helpful Methods that every developer needs.
Show some love and like to support the project #
Say Thanks Here #
Follow Me on Twitter #
Platform Support #
Android | iOS | MacOS | Web | Linux | Windows |
---|---|---|---|---|---|
✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
Installation #
*** follow flutter_local_notifications
installation setup ***
Add this package to pubspec.yaml
as follows:
$ flutter pub add flutter_notification_service
Import package
import 'package:flutter_notification_service/flutter_notification_service.dart';
Initialize flutter_notification_service in main.dart file for initializing Shared Preferences and other variables. Also you can assign your Chat GPT key here.
Contents #
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
Flutter notification services #
/// Pass your audio file 'notification_music.mp3' in res/raw folder and 'app_icon.png' in res/drawable folder inside android/app/src/main then implement below code where you need local notification
late NotificationService _notificationService;
// Create a custom notification configuration (optional)
final NotificationConfig _customConfig = NotificationConfig(
androidAppIcon: 'app_icon', // Custom app icon
assetAppIcon: 'icons/app_icon.png', // Custom asset app icon
androidChannelId: 'my_app_channel',
androidChannelName: 'My App Notifications',
androidChannelDescription: 'Notifications for my awesome app',
notificationSoundResource: 'notification_sound', // Custom sound
);
@override
void initState() {
super.initState();
_initializeNotifications();
}
Future<void> _initializeNotifications() async {
// Initialize notification service with custom config and handlers
_notificationService = NotificationService(config: _customConfig)
..initialize(
onNotificationTap: _handleNotificationTap,
onForegroundMessage: _handleForegroundMessage,
onMessageOpenedApp: _handleMessageOpenedApp,
);
// Check if notifications are enabled
_notificationsEnabled = _notificationService.isNotificationPermissionGranted;
// Get FCM token
_fcmToken = _notificationService.fcmToken;
setState(() {});
}
// Handle local notification tap
void _handleNotificationTap(NotificationResponse response) {
if (kDebugMode) {
print('Notification tapped');
print('Payload: ${response.payload}');
print('Action ID: ${response.actionId}');
}
if (response.payload != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationDetailsPage(
payload: response.payload!,
),
),
);
}
}
// Handle FCM foreground message
void _handleForegroundMessage(RemoteMessage message) {
if (kDebugMode) {
print('Received foreground message:');
print('Title: ${message.notification?.title}');
print('Body: ${message.notification?.body}');
print('Data: ${message.data}');
}
}
// Handle FCM message when app is opened from notification
void _handleMessageOpenedApp(RemoteMessage message) {
if (kDebugMode) {
print('App opened from notification:');
print('Title: ${message.notification?.title}');
print('Body: ${message.notification?.body}');
print('Data: ${message.data}');
}
// Navigate based on the message data
if (message.data['screen'] != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationDetailsPage(
payload: message.data.toString(),
),
),
);
}
}
// Subscribe to FCM topic
Future<void> _subscribeToTopic(String topic) async {
await _notificationService.subscribeToTopic(topic);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Subscribed to topic: $topic')),
);
}
// Unsubscribe from FCM topic
Future<void> _unsubscribeFromTopic(String topic) async {
await _notificationService.unsubscribeFromTopic(topic);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Unsubscribed from topic: $topic')),
);
}
// Example of a notification with Android actions
Future<void> showNotificationWithActions() async {
// Create a notification config with Android actions
final config = NotificationConfig(
androidActionCategories: [
const NotificationCategory(
identifier: 'custom_android_category',
actions: [
NotificationAction(
id: 'custom_action_1',
title: 'Custom Action 1',
iconPath: 'icons/coworker.png'
),
NotificationAction(
id: 'custom_action_2',
title: 'Custom Action 2',
iconPath: 'icons/coworker.png'
),
],
),
],
);
// Initialize the NotificationService with this config
final notificationService = NotificationService(config: config);
// Explicitly process actions
await notificationService.initialize();
// Show the notification with actions
notificationService.showNotification(
title: 'Interactive Notification',
body: 'This notification has multiple actions',
// Optional: Add extra customization
color: Colors.blue,
enableLights: true,
ledColor: Colors.green,
ledOnMs: 1000, // LED on duration in milliseconds
ledOffMs: 500, // LED off duration in milliseconds
);
}
Features and bugs #
Please file feature requests and bugs at the issue tracker.