[英]How can I access my active notifications data?
I need to verify by an ID that comes inside the data field that I received from a firebase message.我需要通过我从 firebase 消息中收到的数据字段中的 ID 进行验证。 How can I access this field based on the active notifications?
如何根据活动通知访问此字段?
The point is to remove the notification once a page with that ID is opened.重点是在打开具有该 ID 的页面后删除通知。
This is what I have to get the notifications这是我必须得到的通知
page.dart页.dart
final List<ActiveNotification>? activeNotifications =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.getActiveNotifications();
that gives me the body, channelId, id, title and hascode.这给了我 body、channelId、id、title 和 hascode。
While RemoteMessage message gives me a lot more stuff including a map data.虽然 RemoteMessage 消息给了我更多的东西,包括 map 数据。
Is there a way to access this data field through the ActiveNotification?有没有办法通过 ActiveNotification 访问这个数据字段?
I'm trying to do the verification with a sample on the body, but it's not a really good pratice giving the circumstances of the project.我正在尝试用身体上的样本进行验证,但考虑到项目的情况,这并不是一个很好的做法。
What I receive from firebase is sent_at (date), service_id (the id I need to get to), id (other id but not so important), body, and title.我从 firebase 收到的是 sent_at(日期)、service_id(我需要到达的 id)、id(其他 id 但不那么重要)、body 和 title。
The service_id shouldn't be displayed in the notification tho, otherwise I'd get it through the notification body service_id 不应该显示在通知中,否则我会通过通知正文获取它
Whoever answered and deleted their answer, helped my a lot.谁回答并删除了他们的答案,对我帮助很大。 So I'm marking this as the solution because it worked.
所以我将其标记为解决方案,因为它有效。 Thank you stranger.
谢谢陌生人。
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final Future<SharedPreferences> _savedNotifications =
SharedPreferences.getInstance();
_savedNotifications.then((saveNotifications) {
saveNotifications.setString(
"service_id_${message.messageId}", message.data["service_id"]);
});
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
onResumed();
break;
case AppLifecycleState.inactive:
onInactive();
break;
case AppLifecycleState.detached:
onDetached();
break;
case AppLifecycleState.paused:
onPaused();
break;
}
}
Future<String?> _getServiceId(title) async {
_savedNotifications.then((saveNotifications) => saveNotifications.reload());
return _savedNotifications.then((saveNotifications) {
_savedNotifications.then(
(value) => value.getKeys().forEach(
(element) async {
if (element.contains('service_id_')) {
String serviceId = value.get(element).toString();
}
},
),
);
});
}
void onResumed() async {
final prefs = await SharedPreferences.getInstance();
prefs.reload();
final List<ActiveNotification>? activeNotifications =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.getActiveNotifications();
for (ActiveNotification notification in activeNotifications!) {
String? serviceId = await _getServiceId(notification.title);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.