86 lines
2.2 KiB
Dart
86 lines
2.2 KiB
Dart
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class NotificationController {
|
|
static final FlutterLocalNotificationsPlugin _notifications =
|
|
FlutterLocalNotificationsPlugin();
|
|
|
|
static Future<void> initialize() async {
|
|
const androidSettings =
|
|
AndroidInitializationSettings('@mipmap/ic_launcher');
|
|
const iosSettings = DarwinInitializationSettings(
|
|
requestAlertPermission: true,
|
|
requestBadgePermission: true,
|
|
requestSoundPermission: true,
|
|
);
|
|
|
|
const initSettings = InitializationSettings(
|
|
android: androidSettings,
|
|
iOS: iosSettings,
|
|
);
|
|
|
|
await _notifications.initialize(
|
|
initSettings,
|
|
onDidReceiveNotificationResponse: (NotificationResponse response) {
|
|
if (response.payload != null) {
|
|
// Handle notification tap
|
|
// You can navigate or perform actions based on the payload
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
static Future<void> showNotification({
|
|
required String title,
|
|
required String body,
|
|
String? payload,
|
|
}) async {
|
|
const androidDetails = AndroidNotificationDetails(
|
|
'basic_channel',
|
|
'Basic notifications',
|
|
channelDescription: 'Notification channel for basic tests',
|
|
importance: Importance.high,
|
|
priority: Priority.high,
|
|
color: Color(0xFF9D50DD),
|
|
enableLights: true,
|
|
enableVibration: true,
|
|
playSound: true,
|
|
);
|
|
|
|
const iosDetails = DarwinNotificationDetails(
|
|
presentAlert: true,
|
|
presentBadge: true,
|
|
presentSound: true,
|
|
);
|
|
|
|
const details = NotificationDetails(
|
|
android: androidDetails,
|
|
iOS: iosDetails,
|
|
);
|
|
|
|
await _notifications.show(
|
|
0,
|
|
title,
|
|
body,
|
|
details,
|
|
payload: payload,
|
|
);
|
|
}
|
|
|
|
static Future<void> requestPermissions() async {
|
|
await _notifications
|
|
.resolvePlatformSpecificImplementation<
|
|
AndroidFlutterLocalNotificationsPlugin>()
|
|
?.requestNotificationsPermission();
|
|
|
|
await _notifications
|
|
.resolvePlatformSpecificImplementation<
|
|
IOSFlutterLocalNotificationsPlugin>()
|
|
?.requestPermissions(
|
|
alert: true,
|
|
badge: true,
|
|
sound: true,
|
|
);
|
|
}
|
|
}
|