So I have a .NET MAUI mobile application targeting Android specifically that has implemented push notifications through the OnMessageReceived()
method and OnNewToken()
using an intent filter and service.
In development when I run the application on a connected device it all works perfectly and OnMessageReceived()
is received as expected.
However, when publishing it to an APK file instead of OnMessageReceived()
being called, a local notification is displayed, even in the foreground. I have no code that enables this feature.
Not sure if this is a .NET MAUI specific issue or something to do with firebase.
My code for receiving notifications is:
[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class PushNotificationFirebaseMessagingService : FirebaseMessagingService
{
/// <summary>
/// Called when a new token is created
/// </summary>
/// <param name="token">The token created</param>
public override void OnNewToken(string token)
{
try
{
// Set the token name to secure storage
SecureStorage.SetAsync(Constants.PushNotificationTokenName, token)
.SafeFireAndForget(false);
}
catch (Exception)
{
// Could be secure storage is not supported
}
}
/// <summary>
/// Called when any new notification hub message is received
/// </summary>
/// <param name="message">The message received</param>
public override void OnMessageReceived(RemoteMessage message)
{
// Code executed on UI here
}
}
}
And my MainActivity.cs code…
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.PostNotifications) != Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.PostNotifications }, 0);
}
CreateNotificationChannelIfNeeded();
}
private void CreateNotificationChannelIfNeeded()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
CreateNotificationChannel();
}
}
private void CreateNotificationChannel()
{
var channelId = $"{PackageName}.general";
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default);
notificationManager.CreateNotificationChannel(channel);
FirebaseCloudMessagingImplementation.ChannelId = channelId;
//FirebaseCloudMessagingImplementation.SmallIconRef = Resource.Drawable.ic_push_small;
}
Things to note:
- I have the permission
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
in my AndroidManifest.xml
2
Answers
Ok so I found the issue, seems to be that having trimming enabled, trims off some functionality of push notifications on publish. So make sure to turn that off by Right click project > Search 'Trim' > Deselect Release
You can first recheck if you have added your SHA-1 key of signature for the release to your firebase console.
For released version, there is a different SHA-1 key .
You can try the following steps:
SHA-1
for releaseSHA-1
to firebase consolegoogle-services.json
.Note: Make sure the key being used by play store to sign your releases is
also added in step 2
There are some similar threads about this problem,you can check them here:
FCM notifications not working in release app
Firebase google authentication not working in release mode