Home > database >  Flutter Local notifications - notification click is not working while the app in background
Flutter Local notifications - notification click is not working while the app in background

Time:01-19

When application killed then notification click not working account to onSelectNotification and also big picture image notification not working when app in background.

flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (action) {})

CodePudding user response:

Handle background messages by registering a onBackgroundMessage handler. When messages are received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running. try this:

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp();

  print("Handling a background message: ${message.messageId}");
}

void main() {
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}

follow full documentation here: https://firebase.google.com/docs/cloud-messaging/flutter/receive

CodePudding user response:

There is no such thing as "in the background" with mobile devices. Mobile devices run one foreground app. When you "put it in the background" it is closed and a screenshot is kept to make you think it's "in the background". It's not. It's closed.

So it does not work when it's closed. That's normal. Because the app isn't running, it cannot execute code.

The app has first to be started again. To find out, whether your app was started by tapping a notification, you can use this line in your start up code:

final NotificationAppLaunchDetails notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

Source: Documentation

This way you can find out if your app was started from your notification and then act accordingly (for example by navigating to a different route depending on those details).

  • Related