TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

No notification sound when IOS app is open #295

Closed mahebisht closed 6 months ago

mahebisht commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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

SgtMadmonkey commented 1 month ago

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