如何在应用程序处于非活动状态时点击UNUserNotification操作按钮中的播放按钮启动音频播放器



嗨,我有一个音频播放器应用程序,它有一个定时器部分。目标是在特定日期和时间启动/停止音频。我已经使用UNUserNotificationCenter实现了这一点。当应用程序处于前台时,这可以很好地工作。但当应用程序处于非活动/背景时,只有当应用程序打开并进入前台时,玩家才会玩游戏。有人能帮我吗。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Local notification received")
let application = UIApplication.shared
print(response.notification.request.content)
let identifier = response.actionIdentifier
let request = response.notification.request
if(identifier == "StartPlaying")
{
AudioManager.sharedInstance.setAudioPlayer(audioUrl: "http://athmaraksha.org/admin/uploads/audio/040D-Daivathinte Vagdhanangal-329.mp3", fromAudio: true)
AudioManager.sharedInstance.player?.play()

}
else if(identifier == "StopPlaying")
{
AudioManager.sharedInstance.player?.pause()
}
completionHandler()

}

当然有多种方法可以做到这一点。我更喜欢的一种方法是使用全局单例,而且非常简单。

所以问题是在后台重新启动应用程序的同时处理推送通知中的数据,对吧?

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)方法中,这就是我们处理推送通知的地方,对吧?

我有一种方法可以检查我当前是否可以推送或显示屏幕(例如,应用程序已完全打开,而不是从后台重新启动(。如果没有办法推送或显示屏幕,因为应用程序只是从后台重新启动(意味着还没有屏幕显示给用户(,那么我会将数据保存到我上面提到的全局单例中。

当根屏幕或主屏幕刚刚完全加载时,这意味着我们准备检查推送通知中是否有保存的数据,也就是说,我们准备根据推送通知进行任何UI更改或显示/推送屏幕。

我希望这个想法能有所帮助!

最新更新