我可以使用
获取通知数据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]
Easy APNs Provider尽管播放自定义声音很好。
答案 0 :(得分:0)
只需从fireBase控制台禁用声音,并在收到通知时使用自定义声音,并在收到通知后调用此方法
导入AVFoundation 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)
}
}