>我可以通过以下方式获取通知数据
let userInfo = notification.request.content.userInfo
但是当我的应用程序在后台运行时,我不知道如何使用它。我从Firebase收到的数据是这样的
%@ [AnyHashable("google.c.a.c_l"): DEFGH, AnyHashable("sound"): alert.aiff, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.a.ts"): 1515722730, AnyHashable("google.c.a.udt"): 0, AnyHashable("gcm.notification.sound2"): default, AnyHashable("gcm.n.e"): 1, AnyHashable("aps"): {
alert = ABC;
badge = 1;
sound = default;
}, AnyHashable("google.c.a.c_id"): xxxxxx, AnyHashable("gcm.message_id"): 0:xx%xx]
简单的APNs提供程序播放自定义声音很好。
只需禁用来自fireBase控制台的声音,并在收到通知时使用自定义声音并在收到通知时调用此方法
导入 AV函数var player: AVAudioPlayer?
func playSound(( {
guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}