58 lines
2.0 KiB
Dart
58 lines
2.0 KiB
Dart
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
class NotificationHelper {
|
|
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
FlutterLocalNotificationsPlugin();
|
|
|
|
Future<void> initialize() async {
|
|
final initializationSettingsAndroid =
|
|
AndroidInitializationSettings('app_icon');
|
|
|
|
final initializationSettings = InitializationSettings(
|
|
android: initializationSettingsAndroid,
|
|
);
|
|
// await flutterLocalNotificationsPlugin.initialize(
|
|
// initializationSettings,
|
|
// onSelectNotification: onSelectNotification,
|
|
// );
|
|
await flutterLocalNotificationsPlugin.initialize(
|
|
initializationSettings,
|
|
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
|
|
);
|
|
}
|
|
|
|
Future<void> showNotification(int id, String title, String body) async {
|
|
const AndroidNotificationDetails androidPlatformChannelSpecifics =
|
|
AndroidNotificationDetails('your channel id', 'your channel name',
|
|
// 'your channel description',
|
|
importance: Importance.max,
|
|
priority: Priority.high);
|
|
const NotificationDetails platformChannelSpecifics =
|
|
NotificationDetails(android: androidPlatformChannelSpecifics);
|
|
await flutterLocalNotificationsPlugin.show(
|
|
id,
|
|
title,
|
|
body,
|
|
platformChannelSpecifics,
|
|
);
|
|
}
|
|
|
|
// Future<void> onDidReceiveNotificationResponse(String? payload) async {
|
|
// if (payload != null) {
|
|
// print('Notification payload: $payload');
|
|
// }
|
|
// }
|
|
|
|
Future<void> onDidReceiveNotificationResponse(
|
|
NotificationResponse response) async {
|
|
// Lakukan sesuatu berdasarkan respons pengguna terhadap pemberitahuan
|
|
// Misalnya, buka layar tertentu atau jalankan tindakan tertentu
|
|
print('Notification tapped! Payload: ${response.payload}');
|
|
}
|
|
|
|
Future<void> onDidReceiveLocalNotification(
|
|
int id, String title, String body, String payload) async {
|
|
// Callback when a local notification is received while the app is in the foreground
|
|
}
|
|
}
|