简体   繁体   中英

Notification is not coming in release mode when flutter app is in background

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GetStorage.init();
  await Firebase.initializeApp();
  await PushNotificationsService().init();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
  setuplocator();
}

Above code is set on main thread. Now the problems is that app not received notification when it is on background. on foreground it works fine. But it is not working in background.

But this all is happening in debug mode and not in release mode. What to do?

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// handle message
}

Just add @pragma('vm:entry-point') on above _firebaseMessagingBackgroundHandler method

I have to note again that this issue only occurs on release builds and only on Android platforms (I have tested this in an emulator with Android 13, as well as a Samsung device running Android 12). In debug and profile mode, the app receives 2 notifications, one from the native Firebase Messaging, and one from the Flutter background handler's firebase_local_notifications, and it should do it in release mode too (this works correctly in 3.0.5), but instead, in 3.3.0 all it shows is the native notification, no trace of the flutter_local_notifications generated one.

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