Newer
Older
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Created by armin on 16.11.17.
//
#include <fstream>
#include <iostream>
#include <fstream>
#include <folly/FileUtil.h>
#include <folly/String.h>
#include <folly/io/async/SSLContext.h>
#include <folly/io/async/SSLOptions.h>
#include <folly/portability/GFlags.h>
#include <proxygen/lib/http/HTTPMessage.h>
#include <proxygen/lib/http/session/HTTPUpstreamSession.h>
#include <proxygen/lib/http/codec/HTTP2Codec.h>
#include <curl/curl.h>
#include "GooglePushModel.h"
std::string GooglePushModel::mApiKey;
void GooglePushModel::loadApiKey() {
std::ifstream ifs("credentials/google/serverKey.txt");
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
GooglePushModel::mApiKey = content;
}
GooglePushModel::GooglePushModel(const std::string &pJson) {
Json::Reader reader;
Json::Value obj;
reader.parse(pJson, obj);
std::cout<<obj["token"].asString()<<std::endl;
if(obj.isMember("token") && obj.isMember("options")){
std::string token = obj["token"].asString();
Json::Value options = obj["options"];
if(options.isMember("from")&&options.isMember("badge")&&options.isMember("title")&&options.isMember("text")&&options.isMember("badge")){
std::cout<<"valid json"<<std::endl;
mTitle = std::move(options["title"].asString());
mText = std::move(options["text"].asString());
mFrom = std::move(options["from"].asString());
mDeviceToken = std::move(obj["token"].asString());
mBadge = options["badge"].asInt();
}
}
}
bool GooglePushModel::sendMessage() {
Json::Value obj;
Json::Value msg;
msg["title"] = mTitle;
msg["body"] = mText;
obj["to"] = mDeviceToken;
obj["notification"] = msg;
Json::FastWriter fast;
std::string data = fast.write(obj);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl){
struct curl_slist *chunk = nullptr;
std::cout<<std::string("Authorization: key="+mApiKey)<<std::endl;
chunk = curl_slist_append(chunk, std::string("Authorization: key="+mApiKey).c_str());
chunk = curl_slist_append(chunk, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL,mApiUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,data.size());
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_USE_SSL, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
res = curl_easy_perform(curl);
if(res != CURLE_OK){
std::cerr<<"curl error: "<<curl_easy_strerror(res)<<std::endl;
}else{
return true;
}
}
return false;
}