【问题标题】:iOS 8 - How to get LocalNotification playing Sound when App is in Background?iOS 8 - 当应用程序处于后台时,如何让 LocalNotification 播放声音?
【发布时间】:2014-11-12 22:24:41
【问题描述】:

我的应用需要有一些闹钟功能。

其中之一应该是在 LocalNotification 触发并且应用程序处于后台时让手机播放声音。

在 iOS 7.1 中一切正常,但在 iOS8 中,在应用返回前台之前不再播放声音。

目前我正在使用AudioToolbox/AudioServices.h 类。

我该如何解决这个问题?

【问题讨论】:

  • 您是否使用registerUserNotificationSettings 注册本地通知。
  • @Bhumit:是的,这样:` if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIRemoteNotificationTypeSound categories:nil ]];}`
  • - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ if (notificationSettings.types == UIUserNotificationTypeNone) { NSLog(@"Denied"); } else{ NSLog(@"Granted"); } }
  • 您确认您在委托回调中被授予使用上述方法的权限?
  • @Bhumit:我也确实在后台模式下进行了检查 -> 检查:模式:音频和 AirPlay 并对 app.plist 进行了设置

标签: ios objective-c uilocalnotification avplayer


【解决方案1】:

此代码可能对您有所帮助...

注册通知设置

- 用于目标 c

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]

        return YES;
    }

- 斯威夫特

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool 
    {
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert |UIUserNotificationType.Badge, categories: nil)   

        return true
    }

【讨论】:

  • 这是我之前写的。请参阅我对 Bhumit 的评论。
【解决方案2】:

这似乎是 Xcode Simulator for iOS8 中的一个错误。 使用适用于 iOS7.1 和/或 iOS8“真实”设备的 Xcode 模拟器,一切正常。

【讨论】:

  • 花了几个小时试图找出声音不工作的原因后,3 台不同的机器(都在模拟器中)。最后,在真机上测试,声音正常。简直难以相信在这么晚的时候在 Xcode 6.4 上出现了这样的问题。简而言之,我可以(在某种程度上)验证它是一个 iOS 8 模拟器错误。
【解决方案3】:

.caf Fine 无法在真实设备上播放本地通知。如果您将 .caf 更改为 .aiff,您会发出声音通知!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多