当我向朋友发送消息时,我正在聊天应用程序中使用Firebase我正在发送推送通知

时间:2018-05-23 07:03:44

标签: iphone swift firebase firebase-realtime-database

我在Swift中遇到以下错误,任何人都可以建议或给出解决方案。

SUCCESS: {
    error =     {
        code = 401;
        message = "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.";
        status = UNAUTHENTICATED;
    };
}

我正在使用firebase发送推送通知。

1 个答案:

答案 0 :(得分:0)

func sendPushNotification(message:String) -> ()
{
        let postParams = ["to":"Set here your receiver push id ","priority":"high","notification":["title":"Title","sound":"default","text":"YourMessage"]] as [String : Any]

        var request = URLRequest(url: URL.init(string: "https://fcm.googleapis.com/fcm/send")!)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("key=getkey form firebase consol", forHTTPHeaderField: "Authorization")

        request.httpBody = try! JSONSerialization.data(withJSONObject: postParams, options: [])

        URLSession.shared.dataTask(with: request, completionHandler: { (responseData, response, responseError) -> Void in
            if response?.StatusCode == 200
            {
            }

        }).resume();
}