Cannot navigate to a specific page when tapping a notification (when app is in background)

731 Views Asked by At

I am developing an app in flutter. I used awesome_notification package to show notifications. In awesome_notification, onActionReceivedMethod works fine when an app is in the foreground but does not work when an app is in the background. How to handle this?

   @pragma("vm:entry-point")
   static Future <void> onActionReceivedMethod(ReceivedAction receivedAction) async {
     // code to navigate some page
   } // not calling when user tap notification

Also, onMessageOpenedApp function is not triggered when tapping the notification. How to solve this? Kindly help me to resolve this.

      FirebaseMessaging.onMessageOpenedApp.listen((message) async {
         // code to navigate some page
      }); // not calling when user tap notification
1

There are 1 best solutions below

0
On

I encountered the same problem with local_notification, to pass an information from the vm entry point to the app.

I solved it by doing an isolate sent / receive like this:

//needed for isolate
import 'dart:isolate';
import 'dart:ui';

//outside main
const String channel = 'channel_key';

@pragma('vm:entry-point')
void onReceiveBackgroundResponse(NotificationResponse notificationResponse) async {
  final sendPort = IsolateNameServer.lookupPortByName(channel);
  sendPort?.send(notificationResponse.actionId);
}

...

//inside main 
void listenNotification() {
  final receivePort = ReceivePort();
  IsolateNameServer.registerPortWithName(receivePort.sendPort, channel);
  receivePort.asBroadcastStream().listen((event) {
    print(event);
  });
}