This is just for Flutter and Android since at the moment I’m exclusively focusing on Android.
I am using the packages firebase_core and firebase_messaging for setting up and triggering push notifications with Firebase.
When I am in debug mode, I can trigger notifications just fine, both in background and foreground.
When I am in release mode, the notifications trigger only in foreground. When in background, no push notification is shown at all. However, _firebaseMessagingBackgroundHandler is being triggered in both debug and release modes!
I have not found anyone with my issue, so it must be some configuration that I’m missing. Just don’t know what it is.
This is my main.dart file:
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('Handling a background message: ${message.messageId}');
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
final settingsController = SettingsController(SettingsService());
await settingsController.loadSettings();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
// Run the app and pass in the SettingsController. The app listens to the
// SettingsController for changes, then passes it further down to the
// SettingsView.
runApp(MyAppWrapper(settingsController: settingsController));
}
// MyAppWrapper to handle MessagingWidget
class MyAppWrapper extends StatelessWidget {
final SettingsController settingsController;
const MyAppWrapper({Key? key, required this.settingsController}) : super(key: key);
@override
Widget build(BuildContext context) {
return MyApp(settingsController: settingsController);
}
}
Can anyone help me?
I tried adding @pragma('vm:entry-point')
before the _firebaseMessagingBackgroundHandler
. While it solved an error related to the background notifications, it did not solve my need to receive push notifications in background in release mode.
flutter clean
flutter pub get
Have been tried as well.
2
Answers
The issue was with ProGuard - even though I didn't have minifyEnabled or shrinkResources enabled to true in the build.gradle file inside the app folder, android was enabling these two configs to true by default.
Setting shrinkResources to false solved the issue.
If you want to keep it enabled to true, you can create a file named proguard-rules.pro file and put this in there: -keep class android.graphics.drawable.Icon { *; }
You can search online for more stuff related to ProGuard issues :)
shrinkResources to false solved the issue.