.NET MAUI Android FireBase service OnMessageReceived strange behavior

52 Views Asked by At

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

0

There are 0 best solutions below