iOS 10用户通知在后台模式下使用自定义声音

11
由于iOS 10中UILocalNotification已经被弃用,因此我使用UNUserNotification框架更新了本地通知流程。
当应用程序在前台时,使用AVPlayer播放自定义声音,这很好用。但是在后台模式下触发本地通知时,播放的是默认通知声音而不是自定义声音。
然而,在iOS9中一切都正常,使用"didReceiveLocalNotification"方法,即使在后台模式下,应用程序也可以播放自定义声音。
更新1:
我正在按以下方式设置本地通知:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("reminder!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Reminder body.", arguments: nil)
if let audioUrl == nil {
    content.sound = UNNotificationSound.defaultSound()
} else {
    let alertSound = NSURL(fileURLWithPath: self.selectedAudioFilePath)
    content.sound = UNNotificationSound(named: alertSound.lastPathComponent!)
}
content.userInfo = infoDic as [NSObject : AnyObject]
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let identifier = "Reminder-\(date)"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
    if error != nil {
        print("local notification created successfully.")
    }
})

我已经查看了许多SO问题,但没有找到解决方案。

非常感激任何帮助!


请展示您尝试过的代码。 - Papershine
你已经解决了它...但是这个问题在不久的将来可能会派上用场:http://stackoverflow.com/questions/42495439/sound-issues-fixes-for-ios-10-push-notifications - mfaani
6个回答

12
在Swift 4.2和Xcode 10.1中,要让本地通知播放声音。
let content = UNMutableNotificationContent()
content.title = "Notification Tutorial"
content.subtitle = "from ioscreator.com"
content.body = " Notification triggered"
//Default sound
content.sound = UNNotificationSound.default
//Play custom sound
content.sound = UNNotificationSound(named:UNNotificationSoundName(rawValue: "123.mp3"))

请确保"123.mp3"文件在您的Bundle中。


1
对于我来说,.mp3格式的文件无法正常工作。但使用.caf格式就可以了。幸运的是,有许多在线转换器可供使用。 - laka

9
发现alertSound没有正确的文件路径,因此content.sound没有设置任何内容。我将录制的文件保存在文档目录中,而实际设备检索到的文件路径与模拟器不同。
将放置在项目捆绑包中的声音文件设置为解决方法。
content.sound = UNNotificationSound(named: "out.caf")

如果我正在下载铃声文件,我应该如何提及路径? - Xcodian Solangi
@XcodianSolangi,看一下这个Gist,是为你创建的,可能会有所帮助:https://gist.github.com/UmairSharif/d3281d147607c825c846b345ffd072d7 - Muhammad Umair

0

我理解

let notificationContent = UNMutableNotificationContent()

...

notificationContent.sound = UNNotificationSound(named: "out.mp3")

目前工作正常,但是:

1)声音文件不应超过30秒(我认为这没问题)

2)声音文件必须是Bundle.mail文件的一部分。如果它存储在其他地方,当处于后台时通知将无法访问它。在前台工作正常。据我所知,苹果不允许访问其他文件夹。

3)声音格式有限制。请参考苹果指南。

4)我不确定Bundle.main中的声音文件是否可以通过编程方式更改。例如:我编写了一个例程,从用户选择的存储在Apple Music中的歌曲中提取30秒...如果信息存储在不同的目录中,则一切正常,但只在前台...一旦锁定或处于后台,就无法访问它。我无法更改/覆盖Bundle.main中的文件。如果可能的话,问题就解决了...但我认为那将是一个安全问题。我得出结论,只要满足上述条件,警报声音和音乐就能正常工作。


如果我正在下载铃声文件,我应该如何提及路径? - Xcodian Solangi
没问题,如果应用程序在前台运行,它可以正常工作。在后台模式下,您可能会遇到与第4点提到的相同问题,因为您存储下载文件的路径将不同于Bundle.main。 - Pe Gra

0
let notificationContent1 = UNMutableNotificationContent()
        notificationContent1.title = "Good morning! Would you like to listen to a morning meditation to start the day fresh ? Or maybe a cool relaxing soundscape? Open the app and browse our collection which is updated daily."
        notificationContent1.subtitle = ""
        notificationContent1.body = ""
        notificationContent1.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "AlarmSound.wav"))

        var dateComponents1 = DateComponents()
        dateComponents1.hour = 08
        dateComponents1.minute = 00

        let trigger1 = UNCalendarNotificationTrigger(dateMatching: dateComponents1, repeats: true)

        let request1 = UNNotificationRequest(
            identifier: UUID().uuidString,
            content: notificationContent1,
            trigger: trigger1
        )

        let notificationContent2 = UNMutableNotificationContent()
        notificationContent2.title = "Good evening! Hope you had a great day. Would you like to meditate on a particular topic ? Or just some relaxing sleep sounds ? Open the app and pick what you want so you can go to sleep relaxed."
        notificationContent2.subtitle = ""
        notificationContent2.body = ""
        notificationContent2.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "AlarmSound.wav"))

        var dateComponents2 = DateComponents()
        dateComponents2.hour = 20
        dateComponents2.minute = 00

        let trigger2 = UNCalendarNotificationTrigger(dateMatching: dateComponents2, repeats: true)

        let request2 = UNNotificationRequest(
            identifier: UUID().uuidString,
            content: notificationContent2,
            trigger: trigger2
        )

        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().add(request1, withCompletionHandler: nil)
        UNUserNotificationCenter.current().add(request2, withCompletionHandler: nil)

0
在您的推送通知包中,您需要包含希望在背景中播放的自定义声音。此声音也需要包含在您的应用程序中。
例如,以下是我如何为iOS使用PHP创建自定义通知包。请注意,在我的通知对象中,我有一个声音对象。
array("to" => $sDeviceID,
                    "data" => array($sMessageType => $sMessage,
                                    "customtitle" => $sMessageTitle,
                                    "custombody" => $sMessage,
                                    "customimage" => "$mIcon") ,
                    "notification" => array("title" => "sMessageTitle",
                                    "text" => $sMessage,
                                    "icon" => "$mIcon",
                                    "sound" => "mySound.mp3"));

感谢您的回复。但是我正在使用LocalNotification而不是推送/远程通知。我的应用程序记录声音并保存它,然后触发本地通知,在后台模式下应该播放。谢谢。 - Muhammad Umair

0

以下是使用UserNotifications框架创建带有自定义声音的通知的方法。

如果notification是您的UNNotificationContent,并且您的自定义媒体文件名为a.mp4,并在您的捆绑包中,只需使用以下代码:

notification.sound = UNNotificationSound(named: "a.mp4")

所以,您不需要使用didRecieveLocalNotification


如果我正在下载铃声文件,我应该如何提及路径? - Xcodian Solangi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接