Flutter firebase messaging v9.0.0 - Not firing events - flutter

I have an app that was using firebase messaging 7.0.3 and all worked perfectly. But when I migrated to firebase messaging 9.0.0, the push notifications are not being handled.
I know that the app is correctly linked to firebase and cloud messaging because in background I see the push notification comming, the problem is when I click that notification the app don't handle this event. Also, when the app is in foreground, the event of receiving the notification is not being triggered.
Specifically, the functions FirebaseMessaging.onMessage and FirebaseMessaging.onMessageOpenedApp are not working. My code is:
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("notification: message");
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("notification: resume");
});
The prints are never called, and if I put some more code inside isn't executed too.
I also call FirebaseMessaging.getToken and I can get the token, plus when the app is in background I get the push notification, so is not a link problem with firebase. In the logs, when I receive the push I can see a message saying: Broadcast received for message. So I assumed that the push is arriving in all the occasions, and I am doing something wrong in the code.
I tested all cases in Android and iOS physical devices.
Someone know why this occurs?

I came across this exact same issue after migrating and upgrading to 9.0.0.
There is a minor issue with 9.0.0 plugin.
The fix can be found here:
https://github.com/FirebaseExtended/flutterfire/issues/4949
To cut a long story short, if you navigate to the definition of the factory
RemoteMessage.fromMap() Using Cmd+Click or Ctrl+Click while hovering over the RemoteMessage class with the mouse), in the return statement, change contentAvailable: map['contentAvailable'], to contentAvailable: map['contentAvailable']??false.
Notifications are now working for me again now.
This should work for you until the plugin is fixed.

Related

FirebaseMessaging getInitialMessage keeps old behavior despite changing the code

I am testing how my Flutter app handles notification using my Samsung Android 12 phone and Firebase Messaging campaign.
I have gotten this code (simplified) to work for a month and notification is handled properly.
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
String? routeName = message.data["route"];
if (routeName != null) {
Navigator.of(context).pushNamed(routeName);
}
}
});
However, today when I wanted to change how handling notification works, I found out that the changes I made to this code are not reflected in the app when debugging through Flutter. Opening the app from a terminated state through the notification still behaves according to the old code above.
I have tried many things:
Uninstalling the app on Android
Removing all code related to Firebase Messaging
Clearing all app cache and data
But the app still keeps the behavior of the old code. Is there anything I can do to reset the behavior of FirebaseMessaging.instance.getInitialMessage() ?

Flutter - myBackgroundMessageHandler never called in push notification

I am using firebase_messaging library with firebase for push notification in flutter. The documentation says that myBackgroundMessageHandler is called when the app is in background. But it has not happened.
I would like to format the title and the body of the notification before it is shown. I am getting notifications when the app is in the background but that particular method is not called. Currently it just shows the exact text sent by backend in title and body.
Is there something I have to do to enable(?) this method? I have it in my fcm.configure method:
_fcm.configure(
onBackgroundMessage: myBackgroundMessageHandler,
)
static Future<Map<String, dynamic>> myBackgroundMessageHandler(Map<dynamic, dynamic> message) async {
//some code here
return message;
}
Thanks in advance!
I had the same problem.
onBackgroundMessage apparently gets triggered only for data-messages.
See the difference here:
https://firebase.google.com/docs/cloud-messaging/concept-options
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
These are handled by the FCM SDK automatically.
Data messages, which
are handled by the client app.
In my case I had to remove the notification (subject, body etc), but also styling like bigpicture completely. Try sending only a data object.

(Ionic) handleNotificationReceived does not get called if the app is killed

I am working on an ionic app and I am using onesignal for push notifications.
The problem I am facing is that the handleNotifiactionRecieved() does not get triggered upon receiving a push notification when the app is not killed (that is removed from the recent apps). Though it works as expected when the app's running but in the background(not inFocus).
setup code is something like this:
if (this.platform.is('cordova')) {
this.oneSignal.startInit('APP_ID');
this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.None);
this.openOneSignalMessage();
this.oneSignal.endInit();
}
my code is something like this:
openOneSignalMessage() {
this.oneSignal.handleNotificationReceived().subscribe((data) => {
this.MsgBody = data.payload.body
console.log('newMessageOneSignal MsgBody', this.MsgBody);
});
}
actual result: handleNotificationReceived() doesn't get called when the app is not running, that is killed.
expected result: handleNotificationReceived() should be called every time a push notification arrives even when the app is not open.
How do I trigger the method every time?
Thanks in advance.
Thanks for pointing out the issue in the docs. They have been updated with the correct info:
handleNotificationReceived (builder method)
Sets a notification received handler. Only called if the app is running in the foreground at the time the notification was received.
If you want to handle from a killed state, make sure to use the handleNotificationOpened method
I find this in the doc:
Sets a notification received handler. Only called if the app is running in the foreground or background at the time the notification was received.
Maybe your app is not working on background.
Or the problem is you need to call ur function after the startinit :
this.onesignal.startInit("YOUR_APPID")
this.onesignal.handleNotificationReceived()

Background notifications using Ionic like a mortal

I have been trying for several months to implement an app that receives notifications whose content I want to be stored in the database of my application. I am using the Firebase Messagging plugin and I am receiving the notifications correctly, both in Foreground and in Background.
However, in the background I am unable to execute my callback without the need to press the notification explicitly by the user. Likewise, there is no way to increase the badge of the application when it is not open.
Now, I do not understand how applications like WhatsApp, Telegram and any other application that is not made by mere mortals work. How is it possible that they can get the data in backgroud, manage the badges, synchronize messages, etc? Being that, supposedly the services like Firebase are limited in background. Even with plugins like Background Mode my application is suspended by Android when the user closes it.
The code that I am currently using to handle notifications is the following:
// In foreground (WORKS)
this.firebaseMessaging.onMessage().subscribe((notificacion) => {
// Insert in DB
...
});
// In background (DOESN'T WORK)
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
// Insert in DB
...
});
What alternative is left? The only thing that occurs to me is to use notifications in Foreground and background only as a warning. So every time I open the application I'll have to call a message synchronization callback with the server (with badge management included).
If someone has some better way, I would greatly appreciate it if you throw a little light on the subject.
From already thank you very much
Ok, after more than a year I think it's time to close this question. I solved the problem this way:
Suppose I need to get a list of messages. This action is made by a function called getMessages()
When a new message is created in the backend, I send a push notification through Firebase service.
If the push notification is received in foreground I refresh call the method directly:
this.firebaseMessaging.onMessage().subscribe((notificacion) => {
getMessages();
});
If the push notification is received in background and the user taps on it there isn't a problem as it's supported by the plugin:
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
getMessages();
});
If the push notification is received in background but the user DOES NOT tap on it and opens the app in another way than the notification, we need to excecute the corresponing function when the app is opened in app.component.ts file:
this.platform.ready().then(() => {
getMessages();
// Extra stuff like this.statusBar.styleDefault(); or this.splashScreen.hide();
});
That way all the cases are considered! You should keep in mind that apps like Whatsapp or Telegram are developed in official technologies like Java or Kotlin which not only have official native plugins, also its code are excecuted natively and not in a limited browser as Cordova or Capacitor frameworks do

Reinstalling the phonegap app causes unable to receive push notifications while app is running

I am creating an app using Phonegap 3.1, Sencha 2.3 for the platform android and iPhone.
For sending notification, i have used pushwoosh service and followed this link (http://www.pushwoosh.com/programming-push-notification/ios/ios-configuration-guide/)
Notification are getting perfectly on both Android and iPhone devices (whether app is running or not).
But the problem is, when I reinstall the app, i am not able to get notifications when the app is running and notifications are getting when app is minimized or not running.
In my index.html file, I have used this code to receive the notification while app is running.
document.addEventListener('push-notification', function(event) {
alert('push-notification');
var notification = event.notification;
navigator.notification.vibrate(1500);
navigator.notification.confirm(
notification.aps.alert, // message
alertDismissed, // callback
'ThisApp', // title
'Done' // buttonName
);
pushNotification.setApplicationIconBadgeNumber(0);
});
function alertDismissed() {
//do something after Done button pressed
}
After reinstall the app this listener is not calling.
I am not getting what actually missing in there, please advise.