-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcmconnector.cpp
45 lines (34 loc) · 1.41 KB
/
gcmconnector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "gcmconnector.h"
#include <QUrl>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkAccessManager>
#include <QSslConfiguration>
#include <QByteArray>
#include <QVariant>
GCMConnector::GCMConnector(){}
void GCMConnector::sendPush(QString& appId, QString& json) {
QUrl url("https://gcm-http.googleapis.com/gcm/send");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QString authorization = "key=" + appId;
request.setRawHeader("Authorization", authorization.toUtf8());
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setProtocol(QSsl::TlsV1_2);
request.setSslConfiguration(config);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(onNetworkResponse(QNetworkReply*)));
QByteArray data = json.toUtf8();
qDebug() << "Sync" << QString::fromUtf8(data.data(), data.size());
manager->post(request, data);
}
void GCMConnector::onNetworkResponse(QNetworkReply* reply) {
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if(statusCode.isValid()){
if(statusCode.toInt() == 200) {
qDebug() << "Success: " << reply->readAll().data();
} else {
qDebug() << "Error: " << reply->readAll().data();
}
}
}