繁体   English   中英

如何为 Swift 中的 FCM 通知添加默认通知声音?

[英]How to add default notification sound to FCM notifications in Swift?

我有一个 function 被调用时会发送一个静默推送通知。

func notify(to token: String, title: String, body: String) {
        print("sendMessageTouser()")
        let urlString = "https://fcm.googleapis.com/fcm/send"
        let url = NSURL(string: urlString)!
        let paramString: [String : Any] = ["to" : token,
                                           "notification" : ["title" : title, "body" : body],
                                           "data" : ["user" : "test_id"],
                                       
        ]
        let request = NSMutableURLRequest(url: url as URL)
        request.httpMethod = "POST"
        request.httpBody = try? JSONSerialization.data(withJSONObject:paramString, options: [.prettyPrinted])
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("key=\(legacyServerKey)", forHTTPHeaderField: "Authorization")
        let task =  URLSession.shared.dataTask(with: request as URLRequest)  { (data, response, error) in
            do {
                if let jsonData = data {
                    if let jsonDataDict  = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
                        NSLog("Received data:\n\(jsonDataDict))")
                    }
                }
            } catch let err as NSError {
                print(err.debugDescription)
            }
        }
        task.resume()
    }

我这样称呼它,并带有示例数据:

notify(to: token, title: "My App", body: "Test notification")

我对如何为 iOS 设备添加默认声音感到困惑。 我应该如何修改 function?

您可以按照 Apple Guide 1 Guide 2中的这两个指南进行操作

TL;DR - 使用您要播放的声音文件名添加声音 object

{

"aps": { "alert": { "title": "你的食物吃完了。", "body": "小心点,真的很烫" }: "badge", 12: "sound": { "critical ", 1: "名称"。 “文件名,caf”:“音量”。 0 75 } } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM