I'm setting up push notifications via FCM and LocalPushNotifications, I was able to set it up in foreground state of app and in the background. In terminated state of the app, I do receive the notification, but when I press on it, no action happens, even though the foreground and background state of the app is working fine and navigates the user to the notification screen, in the terminated state, the app just opens and it doesn't navigate to the notification screen, only opens the main screen of the app. Since the device is not connected I can't see the error inside the console log, but when I start the app from the emulator, this is what I get on start:
I/flutter ( 3829): Got a message whilst in the terminated state!
I/flutter ( 3829): Message data: null
This is called inside the pushNotifications()
method at FirebaseMessaging.instance.getInitialMessage().then()...
Here is the code with comments inside:
Logic for handling push notifications:
Future<void> pushNotifications() async {
await Firebase.initializeApp();
RemoteMessage initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
_handleMessage(initialMessage);
}
/// THIS IS NOT WORKING, IT OPENS THE APP BUT DOESN'T NAVIGATE TO THE DESIRED SCREEN
///gives you the message on which user taps
///and it opened the app from terminated state
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
LocalNotificationService.display(message);
print('Got a message whilst in the terminated state!');
print('Message data: ${message.data}');
if (message != null) {
print('terminated state');
}
});
///forground work
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
LocalNotificationService.display(message);
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
});
///EVEN THOUGH IT IS SUPPOSED TO WORK LIKE THIS, I ONLY MANAGED TO MAKE IT WORK WITH BACKGROUND HANDLER, THIS METHOD NEVER TRIGGERS
///When the app is in background but opened and user taps
///on the notification
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print('Got a message whilst in the background!');
print('Message data: ${message.data}');
_handleMessage(message);
LocalNotificationService.display(message);
});
}
///THIS HANDLES THE NOTIFICATIONS WHEN THE APP IS IN THE BACKGROUND
Future<void> _handleMessage(RemoteMessage message) async {
await Firebase.initializeApp();
if (message.data != null) {
print('message handler');
LocalNotificationService.display(message);/// ALL OF THESE CALLED FROM THE LocalNotificationService CLASS BELOW
}
}
///MAIN METHOD, WHERE I INITIALIZE FIREBASE AND THE METHODES ABOVE(pushNotifications()), HANDLE THE MESSAGES WITH onBackgroundMessage(_handleMessage),
void main() async {
WidgetsFlutterBinding.ensureInitialized();
pushNotifications();
var initializationSettingsAndroid =
AndroidInitializationSettings("@mipmap/ic_launcher");
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
await Firebase.initializeApp();
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
FirebaseMessaging.onBackgroundMessage(_handleMessage);
runApp(MyApp());
}
My local notifications service class:
class LocalNotificationService {
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
static void initialize(BuildContext context) {
final InitializationSettings initializationSettings =
InitializationSettings(
android: AndroidInitializationSettings("@mipmap/ic_launcher"));
_notificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payloadData) async {
if (payloadData!= null) {
Navigator.pushNamed(context, NotificationsScreen.id);
}
});
}
static void display(RemoteMessage message) async {
try {
final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final NotificationDetails notificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
"push notifications",
"push notifications",
"push notifications",
importance: Importance.max,
priority: Priority.high,
));
await _notificationsPlugin.show(
id,
'push notifications',
'You have received a new push notification!',
notificationDetails,
payload: message.data['default'], // THIS IS NULL WHEN IN TERMINATED STATE OF APP
);
} on Exception catch (e) {
print('exception: ' + e.toString());
}
}
}
So like I said, both foreground and background state is working and corresponding to the correct screen, but the terminated app state is not corresponding at all, but it does show the notification and opens the app when tapped on it.
Am I missing something? I mostly followed the documentation and some stuff on my own, but it is still not working as desired.
Any form of help is appreciated, thanks in advance!