How can I play a sound when a push notification is received on my iOS device?

Kim Strasser 661 Reputation points
2023-02-09T13:05:53.2166667+00:00

I use Azure PlayFab to send the push notifications and I want to play a sound if possible when the push notification is received.

I use the following code to display my push notifications on my iOS device:

UserNotificationCenterDelegate.cs:

using System;
using Foundation;
using UserNotifications;

namespace MyProjectiOS
{
    public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {
        [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            completionHandler(UNNotificationPresentationOptions.Banner);
        }
    }
}

Program.cs:

public override void FinishedLaunching(UIApplication app)
{
    UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => {
                // Handle approval  
            });
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
}

How can I play a sound when a push notification is received on my iOS device? I always want to play the same default device sound.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,899 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,446 Reputation points Microsoft Vendor
    2023-02-10T07:04:28.79+00:00

    Hello,

    First, you can change the UNAuthorizationOptions for Sound option in FinishedLaunching method (I also add Badge option)

    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Badge| UNAuthorizationOptions.Sound| UNAuthorizationOptions.Alert , (approved, err) => {
                    // Handle approval  
                });
    

    Then, you can handle Sound option in WillPresentNotification

    completionHandler(UNNotificationPresentationOptions.Sound| UNNotificationPresentationOptions.Badge|UNNotificationPresentationOptions.Banner|UNNotificationPresentationOptions.List);
    

    You are pushing Remote Notifications via Azure PlayFab, so you should check if there is a sound key in the Notification Content, please refer to Generating a remote notification | Apple Developer Documentation, for example:

    {
        "aps":{
            "alert":{
                "title":"Notification Title",
                "subtitle":"Notification Subtitle",
                "body":"This is the message body of the notification."
            },
            "badge":1,
            "sound": "default"
        }
    }
    

    And you need register remote notification in FinishedLaunching method UIApplication.SharedApplication.RegisterForRemoteNotifications ();

    After that, you can uninstall the app and deploy the app on your device again. (Make sure the ability to play sounds has been allowed on your device in Settings->your app->Notification)
    In addition, you can test whether there is sound when you push a LocalNotification, see the invoke platform code to send local notification for testing part in the previous thread. (If there is sound in Local Notification but not Remote Notifications, you might need the further help from Azure PlayFab support. If you have any other issues, please feel free to post here)

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful