I am trying to send a notification/message using firebase cloud function and cloud messaging. The notification works on the background, but in forground is not working.
the following is the python code for the cloud function.
message = messaging.Message(
token=receiver_device_token,
notification=messaging.Notification(
title= '*****',
body=sender + ' sent you a message!'
),
data = {"name": sender ,
"imgURL" : imgURL }
)
I am using the dictionary for data as it is suggested and all the keys and values are string.
In Flutter I have the following basic code added in the void main()
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
however when the message is supposed to be received in forground, I don't get any of the print statements in the onMessage.listen but I get the following in the cosole:
D/FLTFireMsgReceiver(15407): broadcast received for message
I also tried using just a constant string value like "test" insted of my sender adn imgURL variables to make sure that is not the issue. still didn't work
It doesn't make that much sense but just adding the following to the top of it made it work.
FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);