代码之家  ›  专栏  ›  技术社区  ›  Adam

iOS Swift如何在后台播放音频服务系统声音或音频服务系统声音

  •  0
  • Adam  · 技术社区  · 6 年前
    extension AppDelegate : UNUserNotificationCenterDelegate {
    
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    
        let userInfo = notification.request.content.userInfo
    
        print("userNotificationCenter-UNNotificationPresentationOptions")
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        Messaging.messaging().appDidReceiveMessage(userInfo)
    
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
    
        // Print full message.
        print(userInfo)
    
        // Change this to your preferred presentation option
    
    //    AudioServicesPlayAlertSound(1016)
          AudioServicesPlaySystemSound(1016)
          completionHandler([.alert,.badge])
        }
    

    如果播放FCM推送前台音频

    但在后台没有播放音频

    如何玩 AudioServicesPlaySystemSound AudioServicesPlayAlertSound 在后台 已检查功能背景模式

    Swift 4,iOS 11

    1 回复  |  直到 6 年前
        1
  •  0
  •   Martin Prusa    6 年前

    您必须为播放音频设置背景模式,才能执行此操作。在这个答案中有详细的描述 iOS background audio not playing .

    enter image description here