简体   繁体   中英

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. 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.

This is what I have to get the notifications

page.dart

final List<ActiveNotification>? activeNotifications =
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()!
        .getActiveNotifications();

that gives me the body, channelId, id, title and hascode.

While RemoteMessage message gives me a lot more stuff including a map data.

Is there a way to access this data field through the 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.

The service_id shouldn't be displayed in the notification tho, otherwise I'd get it through the notification body

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);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM