警報應用收到 UILocalNotification 後需播放長音樂,但點擊「確定」前不播放,期望**收到即自動播放**。
UILocalNotification 的 soundName **只能播放 ≤ 30 秒** 的音頻文件 (caf/aiff/wav)。notification.soundName = @"short.caf",系統自動播放。didReceiveLocalNotification: 中播放長音樂 (AVAudioPlayer)。PushKit + CallKit 實現來電級界面,支持長鈴聲、後台接聽,但需符合 VoIP 業務場景,濫用會被拒審。- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)note {
if (app.applicationState == UIApplicationStateActive) {
// App 在前台,直接播放長音樂
[self playLongMusic];
} else {
// App 在後台/關閉,用戶點擊通知喚醒,此處會被調用
[self playLongMusic];
}
}
- (void)playLongMusic {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"alarm_long" withExtension:@"mp3"];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player.numberOfLoops = -1;
[self.player play];
}