iOS 10 UserNotifications在后台模式下自定义声音

问题描述 投票:2回答:5

由于UILocalNotification在iOS 10中已弃用,因此我使用UNUserNotification框架更新了本地通知流程。

当应用程序在前台时,应用程序使用AVPlayer播放自定义声音,并且工作正常。但是在触发本地通知的后台模式中,而不是自定义声音,正在播放默认通知声音。

但是,在iOS9中工作正常,使用“didReceiveLocalNotification”方法app即使在后台模式也可以播放自定义声音。

更新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问题,但没有得到解决方案。

任何帮助将不胜感激!

swift ios10 uilocalnotification unusernotificationcenter
5个回答
4
投票

发现alertSound没有正确的文件路径因此content.sound什么也没设置。我将记录的文件保存在文档目录中,与模拟器相比,实际设备的文件路径检索不同。

设置放置在项目包中的声音文件就可以了

content.sound = UNNotificationSound(named: "out.caf")

1
投票

在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.init(named:UNNotificationSoundName(rawValue: "123.mp3"))

0
投票

在推送通知包中,您需要包含要在后台播放的自定义声音。此声音也需要包含在您的应用程序中。

例如,以下是我在PHP for iOS中创建自定义通知包的方法。请注意我的通知对象中有一个声音对象。

array("to" => $sDeviceID,
                    "data" => array($sMessageType => $sMessage,
                                    "customtitle" => $sMessageTitle,
                                    "custombody" => $sMessage,
                                    "customimage" => "$mIcon") ,
                    "notification" => array("title" => "sMessageTitle",
                                    "text" => $sMessage,
                                    "icon" => "$mIcon",
                                    "sound" => "mySound.mp3"));

0
投票

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

如果notification是您的UNNotificationContent,并且您的包中的自定义媒体文件的名称是a.mp4,请使用以下代码:

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

所以,你不需要使用didRecieveLocalNotification


0
投票

我明白那个

let notificationContent = UNMutableNotificationContent()

...

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

工作正常,但是:

1)声音文件不应超过30秒(我认为这是好的)

2)声音文件必须是Bundle.mail文件的一部分。如果它存储在其他地方,则通知在后台时将无法访问它。它在前景中工作正常。据我所知,Apple不提供访问其他文件夹的权限。

3)声音格式有限制。请参阅Apple指南

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

© www.soinside.com 2019 - 2024. All rights reserved.