扑动本地通知不在背景中工作?
我正在使用flutter本地通知在我的应用中显示时间表通知。但是不幸的是,当应用处于终止状态时,它无法正常工作。
这是我的代码:
class Notifications {
static final FlutterLocalNotificationsPlugin _notifications =
FlutterLocalNotificationsPlugin();
static Future<NotificationDetails> _notificationDetails() async {
return const NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'weekly notification channel name',
channelDescription: 'weekly notification description',
playSound: true,
sound: RawResourceAndroidNotificationSound('azan1'),
importance: Importance.high,
),
iOS: IOSNotificationDetails(sound: 'azan1.mp3', presentSound: true));
}
static void init() async {
tz.initializeTimeZones();
const AndroidInitializationSettings android =
AndroidInitializationSettings('@mipmap/ic_launcher');
const IOSInitializationSettings iOS = IOSInitializationSettings();
InitializationSettings settings =
const InitializationSettings(android: android, iOS: iOS);
await _notifications.initialize(settings);
final String locationName = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(locationName));
}
static void showScheduledNotification(
int id, {
required DateTime scheduledDate,
String? title,
String? body,
String? payload,
}) async {
await _notifications.zonedSchedule(
id,
'Azan Time',
'$body Prayer Time',
_scheduleDaily(
Time(scheduledDate.hour, scheduledDate.minute, scheduledDate.second)),
await _notificationDetails(),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
payload: payload,
);
}
static tz.TZDateTime _scheduleDaily(Time time) {
tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime schdeuledDate = tz.TZDateTime(tz.local, now.year, now.month,
now.day, time.hour, time.minute, time.second);
return schdeuledDate.isBefore(now)
? schdeuledDate.add(const Duration(days:1))
: schdeuledDate;
}
static Future<void> cancelNotification(int id) async {
await _notifications.cancel(id);
}
static Future<void> cancelAllNotifications() async {
await _notifications.cancelAll();
}
}
我还在 android.xml 文件中添加了所有属性。 但是,如果有人知道这个问题的解决方案会回答这个问题,那仍然无法正常工作。
I am using flutter local notification to display schedule notification in my app. But unfortunately it is not working when app is in terminated state.
Here is my code:
class Notifications {
static final FlutterLocalNotificationsPlugin _notifications =
FlutterLocalNotificationsPlugin();
static Future<NotificationDetails> _notificationDetails() async {
return const NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'weekly notification channel name',
channelDescription: 'weekly notification description',
playSound: true,
sound: RawResourceAndroidNotificationSound('azan1'),
importance: Importance.high,
),
iOS: IOSNotificationDetails(sound: 'azan1.mp3', presentSound: true));
}
static void init() async {
tz.initializeTimeZones();
const AndroidInitializationSettings android =
AndroidInitializationSettings('@mipmap/ic_launcher');
const IOSInitializationSettings iOS = IOSInitializationSettings();
InitializationSettings settings =
const InitializationSettings(android: android, iOS: iOS);
await _notifications.initialize(settings);
final String locationName = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(locationName));
}
static void showScheduledNotification(
int id, {
required DateTime scheduledDate,
String? title,
String? body,
String? payload,
}) async {
await _notifications.zonedSchedule(
id,
'Azan Time',
'$body Prayer Time',
_scheduleDaily(
Time(scheduledDate.hour, scheduledDate.minute, scheduledDate.second)),
await _notificationDetails(),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
payload: payload,
);
}
static tz.TZDateTime _scheduleDaily(Time time) {
tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime schdeuledDate = tz.TZDateTime(tz.local, now.year, now.month,
now.day, time.hour, time.minute, time.second);
return schdeuledDate.isBefore(now)
? schdeuledDate.add(const Duration(days:1))
: schdeuledDate;
}
static Future<void> cancelNotification(int id) async {
await _notifications.cancel(id);
}
static Future<void> cancelAllNotifications() async {
await _notifications.cancelAll();
}
}
I have also added all properties in Android.xml file.
But still it is not working if anybody know the solution of this problem kindly answer this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
本地通知可能有点棘手。查看
flutter_local_notifications
自述文件:来源:
https://pub.dev/packages/flutter_local_notifications#scheduled-android-notifications
Local notifications might be a bit tricky. Look at the
flutter_local_notifications
README file:Source:
https://pub.dev/packages/flutter_local_notifications#scheduled-android-notifications
根据我的理解,可能是不正确的,在后台工作和终止是两个完全不同的事情。
如果该应用程序被迫辞职并完全死亡,则无法显示局部通知,应使用Firebase推动通知。
有关 applifecyclestate 的更多信息。
另外,我检查了颤音应用程序是否在前景?
According to my understanding which may be incorrect, working in the background and terminated are two completely different things.
If the app is terminated i.e. forced to quit and completely dead, there is no way to show local notifications, firebase push notifications should be used instead.
More info about AppLifecycleState.
Also, How do I check if the Flutter application is in the foreground or not?
如果你能提供你的主要功能,那将会很有帮助。我将向您提供如何创建任何计划通知的一般示例。
在main函数中,初始化NotificationApi如下:
If you could provide your main function, it would have been helpful. I'll give you a general example of how to create any scheduled notification.
On the main function, init the NotificationApi as follows:
在您的application.java中,如果此部分不起作用,则可以将其逃脱以运行该应用程序。
检查清单文件,不要忘记添加
更多查看此 link
In your Application.java if this part not work you can escape it to run the app .
Check the Manifest File and do not forget to add
For More Check This Link
我认为您需要在主要背景服务(如Workmanager)中再次启动它,与应用程序关闭时,您需要在背景中使用它在后面使用的大多数软件包(它是一个分开的环境)
i think you need to init it again in the main background service ( like workmanager ) when using it with the app close you need to init most of the packages you use in the back before using it in the background (its a separated environment)
您是否在
main.dart
中初始化了它?并添加此方法:
}
Did you initialised it in your
main.dart
?And also add this method :
}