using System; using UserNotifications; namespace Other { public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate { private SoundPlayer soundPlayer; public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action completionHandler) { // 显示通知内容 completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound); if(soundPlayer == null) { soundPlayer = new SoundPlayer(); } soundPlayer.PlaySound(); } public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) { // 用户点击通知时停止音频 soundPlayer?.StopSound(); // 处理用户点击通知后的操作 Console.WriteLine("用户点击了通知"); completionHandler(); } } }