wxr
2024-08-02 ea4b1f379fa052b1e044e1d67e85a8500cbd86ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using UserNotifications;
 
namespace Other
{
    public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {
        private SoundPlayer soundPlayer;
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> 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();
        }
 
 
    }
 
}