6

由于UILocalNotification在 iOS 10 中已弃用,因此我使用 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 Questions 但没有得到解决方案。

任何帮助将不胜感激!

4

6 回答 6

8

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

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

content.sound = UNNotificationSound(named: "out.caf")
于 2017-05-13T16:20:24.330 回答
5

在 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"))
于 2019-03-20T10:49:24.593 回答
0

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

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

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

所以,你不需要使用didRecieveLocalNotification.

于 2017-05-13T12:28:32.937 回答
0

我明白那个

let notificationContent = UNMutableNotificationContent()

...

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

工作正常,但是:

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

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

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

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

于 2018-05-06T18:28:24.710 回答
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)
于 2019-11-14T08:33:41.087 回答
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"));
于 2017-05-13T11:44:46.897 回答