Skip to content

[Bug] Foreground notifications in Android not working #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
goatrodeosoftware opened this issue Jan 29, 2025 · 10 comments
Closed

[Bug] Foreground notifications in Android not working #110

goatrodeosoftware opened this issue Jan 29, 2025 · 10 comments
Assignees
Labels
help wanted Extra attention is needed s/needs-info Issue needs more info from the author

Comments

@goatrodeosoftware
Copy link

goatrodeosoftware commented Jan 29, 2025

Description

I've gone through all the issues on this topic that were closed and tried the steps that solved this issue before; frankly, I'd assume I've missed something and maybe will find a way that the instructions/fix can be clearer and not that there's a legit issue, but nonetheless:
I'm able to receive notifications in the background, but in the foreground I neither get a notification nor is the notification received event raised. I'd ask you to just have a quick look and point me in the right direction rather than doing more work yourself than necessary to get me unstuck.

Steps to Reproduce

I've registered in MauiProgram.CreateMauiApp:
.UseFirebasePushNotifications(o => {
o.Android.NotificationChannels = new NotificationChannelRequest[] { new NotificationChannelRequest() {
ChannelId = "1",
ChannelName = "Name",
Description = "ZZZ",
LockscreenVisibility = NotificationVisibility.Public,
Importance = NotificationImportance.High,
IsDefault = true}
};
})
(both with and without the o=> initialization)

{
"message": {
"token": "{{token}}",
"notification": {
"title": "Notification title",
"body": "Notification body"
},
"data": {
"channel_id": "default",
"priority": "high"
},
"android": {
"priority": "high"
}
}
}
(both with and without the android element)

Then wired it up in App.xaml.cs:
protected override async void OnStart()
{
await firebasePushNotification.RegisterForPushNotificationsAsync();
firebasePushNotification.TokenRefreshed += FirebasePushNotification_TokenRefreshed;
firebasePushNotification.NotificationReceived += FirebasePushNotification_NotificationReceived; ;
base.OnStart();
}

private void FirebasePushNotification_NotificationReceived(object sender, FirebasePushNotificationDataEventArgs e)
{
}

private void FirebasePushNotification_TokenRefreshed(object sender, FirebasePushNotificationTokenEventArgs e)
{
}

Expected Behavior

Actual Behavior

Basic Information

  • Version with issue: 3.0.28 on .NET 9
@goatrodeosoftware goatrodeosoftware added the bug Something isn't working label Jan 29, 2025
@thomasgalliker thomasgalliker self-assigned this Jan 30, 2025
@thomasgalliker
Copy link
Owner

I tested the given initialization code with your push notification message and I also don't get a notification while the app is in foreground mode. However, I used the sample app in this repository (which is .NET 8). I can confirm that it works in the sample app.

  • Your message sends a "high" priority notification to Android. This instructs the library to display a notification popup (instead of raising the NotificationReceived event).
  • If the user taps on the notification poup (or on the notification in the notification tray) you'll receive a NotificationOpened event.

So, from the library's point of view, everything seems fine.

If you provide a small reproduction sample app, I might be able to provide further help.

@thomasgalliker thomasgalliker added the s/needs-info Issue needs more info from the author label Feb 16, 2025
@thomasgalliker thomasgalliker added help wanted Extra attention is needed and removed bug Something isn't working labels Feb 25, 2025
@thomasgalliker
Copy link
Owner

I guess this is a bug. Can you try the latest 3.1.x-pre nuget? There is a major fix and a big refactoribg in this build that deals with Android notification handling.

@thomasgalliker
Copy link
Owner

@goatrodeosoftware can you try the latest stable release 3.1.27 and let me know if this problem still exists?

@goatrodeosoftware
Copy link
Author

Hi Thomas- sorry, I moved on to another library for firebase.

@REDECODE
Copy link

Hello Thomas, i have the same problem with foreground notification not shown on android 10.0 device.
First i try your new version 3.1.27 but it crash on MainActivity OnCreate on the line

base.OnCreate(savedInstanceState);

with the error:

System.ArgumentException: 'SetNotificationChannels failed: notificationChannelRequests does not contain any active NotificationChannelRequest with IsDefault=true (Parameter 'notificationChannelRequests')'

I see that on your sample app you use o.Android.NotificationChannels = NotificationChannelSamples.GetAll().ToArray();

but because i prefer to not add "sample code" that maybe works i try to downgrade to the previous version 3.0.28 and it run correctly except foreground notification.

Is mandatory to set NotificationChannels ? there is a default implementation without adding sample code? I need that user can upgrade the app from the previous one to this one without changing anything on notification and using the default channel (and receive notification using topic)

Thanks

@KotM
Copy link

KotM commented May 5, 2025

@thomasgalliker I can confirm the issue reported by @REDECODE above.

In scope of migrating to .NET 9 I tried to update the plugin from version 3.0.28 to the latest 3.1.27 and encountered with the failure on start:

**System.ArgumentException:** 'SetNotificationChannels failed: notificationChannelRequests does not contain any active NotificationChannelRequest with IsDefault=true (Parameter 'notificationChannelRequests')'

 	0x2BA in Plugin.FirebasePushNotifications.Platforms.Channels.NotificationChannels.EnsureNotificationChannelRequests	C#
 	0x94 in Plugin.FirebasePushNotifications.Platforms.Channels.NotificationChannels.SetNotificationChannels	C#
 	0x46 in Plugin.FirebasePushNotifications.Platforms.FirebasePushNotificationManager.ConfigurePlatform	C#
 	0x1E in Plugin.FirebasePushNotifications.Platforms.FirebasePushNotificationManager..ctor	C#
 	0x79 in Plugin.FirebasePushNotifications.CrossFirebasePushNotification.CreateFirebasePushNotification	C#
 	0x14 in System.Lazy<Plugin.FirebasePushNotifications.IFirebasePushNotification>.PublicationOnlyViaFactory	C#
 	0x5B in System.Lazy<Plugin.FirebasePushNotifications.IFirebasePushNotification>.CreateValue	C#
 	0xB in System.Lazy<Plugin.FirebasePushNotifications.IFirebasePushNotification>.get_Value	C#
 	0x5 in Plugin.FirebasePushNotifications.CrossFirebasePushNotification.get_Current	C#
 	0x0 in Plugin.FirebasePushNotifications.IFirebasePushNotification..cctor	C#
 	0x0 in Plugin.FirebasePushNotifications.MauiAppBuilderExtensions.<>c.<UseFirebasePushNotifications>b__0_1	C#
 	0xD in Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory	C#
...

The issue has disappeared only when I added o.Android.NotificationChannels = [.. NotificationChannelSamples.GetAll()]; from your sample application (with NotificationChannelSamples class).

@REDECODE
Copy link

REDECODE commented May 7, 2025

I found the problem.
I need to set o.Android.DefaultNotificationImportance = NotificationImportance.High or above to show all notification even in foreground.

PS: another possible solution is to not set the DefaultNotificationImportance but set the priority correcty when i send the notification from the API in the Data section.

var message = new Message()
{    
    Android = new AndroidConfig
    {
        Priority = Priority.High, //--- NOT WORKING 
        Notification = new AndroidNotification
        {
            Sound = "default",
        }
    },
    Data = new Dictionary<string, string>()
    {
        { "priority", "high" }   //--- WHEN I ADD THIS LINE NOTIFICATION START WORKING CORRECTLY
    },

@thomasgalliker
Copy link
Owner

thomasgalliker commented May 7, 2025

Ahh, now I understand what you mean @REDECODE :) Aww, yes. Just to clarify: NotificationReceived event is fired when the Android app runs in foreground AND you do not set the priority to high, correct? That would be how it's designed to work. As soon as you receive a notification with "high priority" the notification popup is also shown when the app runs in foreground mode.

One more thing to add: You can also set this as a default behavior in UseFirebasePushNotifications you can set the options to
o.Android.DefaultNotificationImportance = NotificationImportance.High;. Have a look at the sample app's MauiProgram.cs. This way, you don't need to send priority flags around with each push notification message.

@thomasgalliker
Copy link
Owner

The System.ArgumentException 'SetNotificationChannels failed: notificationChannelRequests does not contain any active NotificationChannelRequest with IsDefault=true (Parameter 'notificationChannelRequests') mentioned by @KotM is a different problem. It's actually not a problem, it is absolutely intended. I changed something in the internal design of how notification channels in android are handled. Obviously, many devs struggle with this as it seems to be a not very helpful exception message. I'll check if I can bring back a default notification channel again (if none is created by the dev at startup).

@thomasgalliker
Copy link
Owner

@KotM the exception regarding 'missing default notification channel' should be gone in latest pre-release (currently 3.2.1-pre). I this version, I always create a default notification channel if none is specified in the Android-specific options (o.Android.NotificationChannels). The MauiProgram.cs of the sample app has some commented-out code which demonstrates the use of Android/iOS specific options.

Feedback is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed s/needs-info Issue needs more info from the author
Projects
None yet
Development

No branches or pull requests

4 participants