admin管理员组文章数量:1332383
Im trying to show a text every x hours in android notification. I asked ChatGpt as my only source to write it but it seems it is missing something i can't quite figure out. this is what i've done so far:
main.dart:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> main() async {
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
}
WidgetsFlutterBinding.ensureInitialized();
tz.initializeTimeZones();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
const InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
if (Platform.isAndroid || Platform.isIOS) {
await NotificationService().scheduleRepeatingNotification();
}
runApp(const MyApp());
}
notification_service.dart
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
class NotificationService {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> scheduleRepeatingNotification() async {
const AndroidNotificationDetails androidDetails =
AndroidNotificationDetails(
'channel_id',
'channel_name',
channelDescription: 'description',
importance: Importance.max,
priority: Priority.high,
);
const NotificationDetails platformDetails = NotificationDetails(
android: androidDetails,
);
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
final tz.TZDateTime firstNotification =
now.add(const Duration(hours: 3)); // Start in 3 hours
await flutterLocalNotificationsPlugin.zonedSchedule(
0, // ID of the notification
'Scheduled Notification',
'This notification repeats every 3 hours.',
firstNotification,
platformDetails,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time, // To repeat at intervals
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, // Add this
);
}
}
pubspec.yaml:
dependencies:
flutter:
sdk: flutter
provider: ^6.0.1
sqflite: ^2.0.3
path: ^1.8.3
sqflite_common_ffi: ^2.1.0
flutter_local_notifications: ^18.0.1
get: ^4.6.5
cupertino_icons: ^1.0.8
timezone: ^0.9.0
now what happens when i install it on my phone is that it just dies. app's screen is black and it is stuck. is there anything wrong i'm doing?
Im trying to show a text every x hours in android notification. I asked ChatGpt as my only source to write it but it seems it is missing something i can't quite figure out. this is what i've done so far:
main.dart:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> main() async {
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
}
WidgetsFlutterBinding.ensureInitialized();
tz.initializeTimeZones();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
const InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
if (Platform.isAndroid || Platform.isIOS) {
await NotificationService().scheduleRepeatingNotification();
}
runApp(const MyApp());
}
notification_service.dart
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
class NotificationService {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> scheduleRepeatingNotification() async {
const AndroidNotificationDetails androidDetails =
AndroidNotificationDetails(
'channel_id',
'channel_name',
channelDescription: 'description',
importance: Importance.max,
priority: Priority.high,
);
const NotificationDetails platformDetails = NotificationDetails(
android: androidDetails,
);
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
final tz.TZDateTime firstNotification =
now.add(const Duration(hours: 3)); // Start in 3 hours
await flutterLocalNotificationsPlugin.zonedSchedule(
0, // ID of the notification
'Scheduled Notification',
'This notification repeats every 3 hours.',
firstNotification,
platformDetails,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time, // To repeat at intervals
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, // Add this
);
}
}
pubspec.yaml:
dependencies:
flutter:
sdk: flutter
provider: ^6.0.1
sqflite: ^2.0.3
path: ^1.8.3
sqflite_common_ffi: ^2.1.0
flutter_local_notifications: ^18.0.1
get: ^4.6.5
cupertino_icons: ^1.0.8
timezone: ^0.9.0
now what happens when i install it on my phone is that it just dies. app's screen is black and it is stuck. is there anything wrong i'm doing?
Share Improve this question asked Nov 21, 2024 at 8:14 Ali.RashidiAli.Rashidi 1,4626 gold badges28 silver badges58 bronze badges1 Answer
Reset to default 0have you added POST_NOTIFICATIONS permission ?, becuase posting notifications on Android. In recent Android versions (starting from Android 13, API level 33), you need to explicitly declare the POST_NOTIFICATIONS permission in your AndroidManifest.xml like below:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Full documentation is available here
本文标签: dartDisplaying a notification every x hour in flutterStack Overflow
版权声明:本文标题:dart - Displaying a notification every x hour in flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742303231a2449406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论