I am getting a strange behavior of the FireBaseService implementation on Android.
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
badgeNumber++;
var notification = message.GetNotification();
SendNotification(notification.Body, notification.Title, message.Data);
}
private void SendNotification(string messageBody, string title, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.AddFlags(ActivityFlags.SingleTop);
foreach (var key in data.Keys)
{
var value = data[key];
intent.PutExtra(key, value);
}
var pendingIntent = PendingIntent.GetActivity(this,MainActivity.NotificationId, intent, PendingIntentFlags.OneShot | PendingIntentFlags.Immutable);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.ChannelId)
.SetContentTitle(title)
.SetSmallIcon(Microsoft.Maui.Controls.Resource.Drawable.ic_notification)
.SetContentText(messageBody)
.SetChannelId(MainActivity.ChannelId)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true)
.SetPriority((int)NotificationPriority.Max);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NotificationId, notificationBuilder.Build());
}
The push notification is received, and when I debug using a connected device, I can see that the message is received, and SendNotification function is get called. And then comes the funny part. When the foreach finishes its executio the SendNotification jumbs back to the OnMessageReceived function and the code never reaches the part after the foreach in the SendMessage function.
I tried to wrap everything in a try-catch to see, can be some error, but no errors.
thnx
-
Do you mean to say that the notifications didn't show up properly?Jessie Zhang -MSFT– Jessie Zhang -MSFT2024年02月26日 08:01:24 +00:00Commented Feb 26, 2024 at 8:01
1 Answer 1
I experienced the same issue as you.
In devices connected in Debug Mode, FCM's OnNewToken() and OnMessageReceived() were called successfully, but in the Release Mode app, they were not invoked.
This is because, starting from .NET 7.x, unused code is trimmed during the build process due to the use of the linker. FCM Firebase relies on dynamic linking and calls functions later when events occur. However, during compilation, the linker considers these functions unnecessary and removes them to optimize the app.
Although there isn't an exact solution yet, you can resolve this issue by adding the following settings to your .csproj file. Add or modify the following within the <PropertyGroup> section:
<RunAOTCompilation>False</RunAOTCompilation><br>
<PublishTrimmed>False</PublishTrimmed>
This disables trimming during deployment, ensuring that dynamically called Firebase functions are retained. However, keep in mind that the size of the deployed app will increase as it won't be optimized.
Refer to the following link for more details:
Comments
Explore related questions
See similar questions with these tags.