-
Notifications
You must be signed in to change notification settings - Fork 1
/
qparse_android.cpp
60 lines (54 loc) · 2.15 KB
/
qparse_android.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "qparse.h"
#include <QNetworkAccessManager>
#include <QtAndroid>
#include <QtAndroidExtras>
#include <QtConcurrent>
void QParse::createInstallation( QStringList channels, QString timeZone ) {
QAndroidJniObject activity = QtAndroid::androidActivity();
// Retrieve the deviceToken
QAndroidJniObject instanceId = QAndroidJniObject::callStaticObjectMethod(
"com/google/android/gms/iid/InstanceID",
"getInstance",
"(Landroid/content/Context;)Lcom/google/android/gms/iid/InstanceID;", activity.object<jobject>());
QAndroidJniObject senderId = QAndroidJniObject::fromString(gcmSenderId);
QAndroidJniObject scope = QAndroidJniObject::fromString("GCM");
QAndroidJniObject token = instanceId.callObjectMethod(
"getToken",
"(Ljava/lang/String;Ljava/lang/String;Landroid/os/Bundle;)Ljava/lang/String;",
senderId.object<jstring>(), scope.object<jstring>(), NULL);
deviceToken = token.toString();
qDebug() << "TOKEN " << deviceToken;
QAndroidJniEnvironment env;
if (env->ExceptionCheck()) {
// Handle exception here.
env->ExceptionClear();
}
if ( installation.contains("deviceToken") ) {
QString currDeviceToken = installation["deviceToken"].toString();
if ( deviceToken == currDeviceToken ) {
qDebug() << "VALID DEVICE TOKEN";
return;
} else {
installation = QJsonObject();
qDebug() << "INVALID DEVICE TOKEN: CREATE A NEW INSTALLATION ROW";
}
}
// Prepare the request
// direct request, the reply will be handled on onRequestFinished
QNetworkRequest request(QUrl("https://api.parse.com/1/installations"));
request.setRawHeader("X-Parse-Application-Id", appId.toLatin1());
request.setRawHeader("X-Parse-REST-API-Key", restKey.toLatin1());
request.setRawHeader("Content-Type", "application/json");
installation["deviceType"] = "android";
installation["pushType"] = "gcm";
installation["deviceToken"] = deviceToken;
installation["GCMSenderId"] = gcmSenderId;
if ( !channels.isEmpty() ) {
installation["channels"] = QJsonArray::fromStringList(channels);
}
if ( !timeZone.isEmpty() ) {
installation["timeZone"] = timeZone;
}
QJsonDocument jsonDoc(installation);
net->post( request, jsonDoc.toJson(QJsonDocument::Compact) );
}