Skip to content
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

No notification sound when IOS app is open #295

Closed
mahebisht opened this issue May 7, 2024 · 3 comments
Closed

No notification sound when IOS app is open #295

mahebisht opened this issue May 7, 2024 · 3 comments

Comments

@mahebisht
Copy link

mahebisht commented May 7, 2024

I have a .NET MAUI 8 App. There is sound with notification when app is close and in background.

Notification is visible but no sound when app is running in IOS. I am testing with my mobile, IOS version is 16.6.1

Following is the code to send the message from web.

Dim notificationMessage As Message
If isAndroid Then
notificationMessage = New Message With {
.Token = deviceToken
}
Else
notificationMessage = New Message With {
.Token = deviceToken,
.Apns = New ApnsConfig With {
.Aps = New Aps With {
.Sound = "Default"
}
}
}
End If

@mahebisht mahebisht changed the title No notification sound when IOS app is running No notification sound when IOS app is open May 7, 2024
@mahebisht
Copy link
Author

mahebisht commented May 13, 2024

I am able to fix the notification sound when app is open using the following code.

void NotificationReceived(object source, FCMNotificationReceivedEventArgs e)
{
#if IOS
SystemSound sound = new SystemSound(1007);
sound.PlaySystemSound();
#endif

}

@mahebisht
Copy link
Author

I am able to fix the notification sound when app is open in IOS

@SgtMadmonkey
Copy link

I ran into this issue while working on one of my projects, and managed to find the fix for it.

The issue stems from a missing flag in the iOS FirebaseCloudMessagingImplementation's WillPresentNotification delegate.
Namely, the completionHandler is missing the UNNotificationPresentationOptions.Sound flag.

A temporary workaround for this can be made by manually overriding the UNUserNotificationCenter delegate.

public class FirebaseDelegate : NSObject, IUNUserNotificationCenterDelegate
{
	private static FirebaseDelegate instance = null;

	public static FirebaseDelegate Instance
	{
		get => instance ??= new();
	}

	FirebaseDelegate(){}

	public void Initialize()
	{
		UNUserNotificationCenter.Current.Delegate = this;
	}

	[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
	public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
	{
		var fcmNotification = notification.ToFCMNotification();
		((FirebaseCloudMessagingImplementation)CrossFirebaseCloudMessaging.Current).OnNotificationReceived(fcmNotification);

		if (!fcmNotification.IsSilentInForeground)
		{
			if (OperatingSystem.IsIOSVersionAtLeast(14))
			{
				completionHandler(UNNotificationPresentationOptions.Banner 
					| UNNotificationPresentationOptions.List 
					| UNNotificationPresentationOptions.Sound);

			}
			else
			{
				completionHandler( UNNotificationPresentationOptions.Alert
					| UNNotificationPresentationOptions.Sound);
			}
		}
	}

	[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
	public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
	{
		((FirebaseCloudMessagingImplementation)CrossFirebaseCloudMessaging.Current).DidReceiveNotificationResponse(center, response, completionHandler);
	}
}

And then implement it after your Plugin.Firebase implementation in your MauiProgram.cs

	FirebaseCloudMessagingImplementation.Initialize();
	FirebaseDelegate.Instance.Initialize();

Do note that the payload needs to contain the sound key in the apns payload for this to work:

"message": {
  ...
  "apns": {
    "payload": {
      "aps": {
        "sound" : "default"
      }
    }
  }
}

I will submit a pull request with the proposed fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants