When i install my flutter app on Android devices, the onBackgroundMessage() doesn't work. Someone knows how to solve?

Viewed 34

I'm using onBackgroundMessage() and FirebaseMessaging.onMessage.listen() to receive notifications both in Background and Foreground mode, but i only get the notifications in Foreground. Here's a snippet of my app.dart:

import 'package:souhi/services/firebase_messaging.service.dart';

import 'package:souhi/services/notification.service.dart';

class App extends StatefulWidget { const App({

```super.key,

});

@override

State<App> createState() => _AppState(); }

class _AppState extends State<App> { bool isAlertOnDialogOpen = false; @override void initState() { super.initState();

initializeFirebaseMessaging();
checkNotifications();

Connectivity().onConnectivityChanged.listen((event) async {
  await showConnectivityMessage(event);
});

}

Widget build(BuildContext context) {

 ...//skipping this part

}

initializeFirebaseMessaging() async {

await Provider.of<FirebaseMessagingService>(context, listen: false)
    .initialize();

}

checkNotifications() async {

await Provider.of<NotificationService>(context, listen: false)
    .checkForNotifications();

}

}```

My App.dart uses 2 services: FirebaseMessagingService and NotificationService. See that on FirebaseMessagingService i got thoses calls:

Future<void> _onBackgroundMessage(RemoteMessage message) async {

print('_onBackgroundMessage');

//   name: 'Monitorer',
//   options: DefaultFirebaseOptions.currentPlatform,
// );
notificationService.showNotification(message);```
}

class FirebaseMessagingService {


final NotificationService _notificationService;

FirebaseMessagingService(this._notificationService) {

  notificationService = _notificationService;
}

Future<void> initialize() async {

  print('firebase_messaging initialize');

  await FirebaseMessaging.instance
      .setForegroundNotificationPresentationOptions(
    badge: true,
    sound: true,
    alert: true,
  );
  var app = await Firebase.initializeApp(
    name: 'Monitorer',
    options: DefaultFirebaseOptions.currentPlatform,
  );

  onBackgroundMessage();
  _onMessage();

  
}


_onMessage() async {
  print('firebase_messaging _onMessage');

  FirebaseMessaging.onMessage.listen((RemoteMessage message) async {

    print('firebase_messaging onMessage listen');

    _notificationService.showNotification(message);
  });
}

onBackgroundMessage() {

  print('firebase_messaging _onBackgroundMessage');
  FirebaseMessaging.onBackgroundMessage(_onBackgroundMessage);
}```



I've tried so many things. I'm using all the permissions in my AndroidManifest.xml and even this way, it didn't work out: 

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
0 Answers
Related