[英]Flutter Local Notifications doesn't execute code with app terminated
我有一個安排警報的應用程序,它會觸發 flutter 本地通知。 當按下這些通知上的按鈕之一時,數據庫會更新以反映您已關閉警報。
但是,當應用程序終止/關閉時,似乎沒有執行任何代碼並且數據庫沒有得到更新。 通知確實會被觸發,當用戶按下其中的按鈕時,不會執行任何代碼。 看起來像這樣
我想知道我正在嘗試的是否可能,因為在這一點上我不確定。
通知是使用 flutter 本地通知插件中的 zonedSchedule 創建的。
按下通知中的按鈕之一時,應用程序不會打開。
代碼如下:
class LocalNoticationsService {
LocalNoticationsService(
this._notificationsPlugin,
);
final FlutterLocalNotificationsPlugin _notificationsPlugin;
Future<void> init() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings(
'img/path',// path to notification icon
);
final DarwinInitializationSettings initializationSettingsIOS = DarwinInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
defaultPresentAlert: true,
defaultPresentSound: true,
notificationCategories: [
DarwinNotificationCategory(
'category',
options: {
DarwinNotificationCategoryOption.allowAnnouncement,
},
actions: [
DarwinNotificationAction.plain(
'snoozeAction',
'snooze',
),
DarwinNotificationAction.plain(
'confirmAction',
'confirm',
options: {
DarwinNotificationActionOption.authenticationRequired,
},
),
],
),
],
);
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
await _notificationsPlugin.initialize(
initializationSettings,
onSelectNotification: _onSelectedNotification,
onSelectNotificationAction: onSelectNotificationAction,
);
final notificationOnLaunchDetails = await _notificationsPlugin.getNotificationAppLaunchDetails();
if (notificationOnLaunchDetails?.didNotificationLaunchApp ?? false) {
_onSelectedNotification(notificationOnLaunchDetails!.payload);
}
}
void _onSelectedNotification(String? payload) {
// Not relevant to this problem
}
Future<bool> _confirmAlarm(AlarmPayLoad alarmPayload) async {
// Update database
}
}
// Top level function
Future<void> onSelectNotificationAction(NotificationActionDetails details) async {
await Firebase.initializeApp();
final localNotificationsService = LocalNoticationsService(
FlutterLocalNotificationsPlugin()
);
await localNotificationsService.init();
final alarmPayload = AlarmPayLoad.fromJson(details.payload);
await localNotificationsService._confirmAlarm(alarmPayload)
}
由於這一切都在后台運行,我一直無法找到記錄事情的方法。 我嘗試使用 http package 將發布請求發送到帶有一些數據的 webhook,但是這些似乎在應用程序終止時不會觸發,但是當應用程序處於前台或后台時(未關閉)。
我在網上找到的關於 flutter 本地通知的任何帖子或視頻似乎都沒有涵蓋這個問題,或者正在使用 firebase 推送通知和相關的 package 進行后台處理。
我很想聽聽有關可能出了什么問題或可能是什么解決方案以及如何處理后台通知的任何反饋。 如果您需要有關代碼的其他信息,請隨時詢問。
如果您想在后台運行它,請點擊此處的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.