FirebaseMessaging getInitialMessage keeps old behavior despite changing the code - flutter

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() ?

Related

Run terminated flutter app from notification

I'm sending FCM notifications from Kotlin native code in a Flutter app.
How can I open a Flutter terminated app by clicking on the notification that I have send through Kotlin native code?
Is there any way to achieve it?
Solution: I have solved it by using
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) { // your logic }
});

Flutter firebase messaging v9.0.0 - Not firing events

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.

Why is onTokenRefresh not firing?

I'm new to firebase messaging and flutter. According to the flutter firebase_messaging package docs, onTokenRefresh is fired when a new FCM token is generated. And according to Google's firebase docs there are two scenarios that triggers token generation:
When a new token is generated on initial app startup
Whenever an existing token is changed
Here is a simplified version of the main function of my application. After each execution, I delete the app from the emulator and the displayed token does indeed change. Despite this, onTokenRefresh is never fired and it should if my understanding of the documentation is correct.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.instance.onTokenRefresh.listen((String token) {
print("New token: $token");
});
String token = await FirebaseMessaging.instance.getToken();
print("Token: $token");
//runApp(MyApp());
}
As I said, I'm new to flutter, dart and firebase messaging, is there something I'm fundamentally misunderstanding? Thanks.
So I think I figured it out. I noticed that sometimes, the onTokenRefresh does indeed fire. And I was wondering if it had something to do with how the flutter application is launched onto the emulator, in the sense that there is a race condition between when the token is generated and the listener attached.
To get the app to appear to start for the first time, I wiped the app data. Unfortunately this causes the flutter to automatically disconnect from the app which means I won't see the output of the print statement. So instead of trying to print when the token generation occurs, I assigned a value from the onTokenRefresh listener to a variable. I then updated a text widget with the value of the variable. And onTokenRefresh does indeed fire each time at start up if the app data has previously been wiped.

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.