Skip to content
Snippets Groups Projects
ApplePushModel.cpp 4.68 KiB
Newer Older
Armin Felder's avatar
Armin Felder committed
/********************************************************************************************************************
 *                                                                                                                  *
 * Copyright (C) 2017 Armin Felder                                                                                  *
 * This file is part of RocketChatMobilePushGateway <https://git.fairkom.net/chat/RocketChatMobilePushGateway>.     *
 *                                                                                                                  *
 * RocketChatMobilePushGateway is free software: you can redistribute it and/or modify                              *
 * it under the terms of the GNU General Public License as published by                                             *
 * the Free Software Foundation, either version 3 of the License, or                                                *
 * (at your option) any later version.                                                                              *
 *                                                                                                                  *
 * RocketChatMobilePushGateway is distributed in the hope that it will be useful,                                   *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                                                   *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                                    *
 * GNU General Public License for more details.                                                                     *
 *                                                                                                                  *
 * You should have received a copy of the GNU General Public License                                                *
 * along with RocketChatMobilePushGateway. If not, see <http://www.gnu.org/licenses/>.                              *
 *                                                                                                                  *
 ********************************************************************************************************************/
Armin Felder's avatar
Armin Felder committed
#include <jsoncpp/json/json.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "ApplePushModel.h"
Armin Felder's avatar
Armin Felder committed


Armin Felder's avatar
Armin Felder committed
ApplePushModel::ApplePushModel(const std::string &pJson):mPusher("/certs/apple/cred.pem") {
Armin Felder's avatar
Armin Felder committed
    Json::Reader reader;
    Json::Value obj;
    Json::FastWriter fast;

    if(pJson.length()) {
        reader.parse(pJson, obj);

        if (obj.isMember("token") && obj.isMember("options")) {

            Json::Value options = obj["options"];

            Json::Value apn = options["apn"];
            std::string test(fast.write(options));
            if (apn.isMember("text")) {
               std::string temp = std::move(apn["text"].asString());
                unsigned long index = 0;
                while(true){
                    index = temp.find('\n',index);
                    if(index == std::string::npos){
                        break;
                    }
                    temp.replace(index, 1, "\\r\\n");
                    index += 4;
                }
                mApn = std::move(temp);


            }
            if (options.isMember("title")) {
                mTitle = std::move(options["title"].asString());
            }
            if (options.isMember("text")) {
                mText = std::move(options["text"].asString());
            }
            if (options.isMember("from")) {
                mFrom = std::move(options["from"].asString());
            }
            if (options.isMember("badge")) {
                mBadge = options["badge"].asInt();
            }
            if (options.isMember("payload")) {
                mPayload = std::move(fast.write(options["payload"]));
            }
            if (options.isMember("topic")) {
                mTopic = std::move(options["topic"].asString());
            }
            if (options.isMember("sound")) {
                mSound = std::move(options["sound"].asString());
            }
Armin Felder's avatar
Armin Felder committed
            mDeviceToken = std::move(obj["token"].asString());
Armin Felder's avatar
Armin Felder committed
        }
    }
Armin Felder's avatar
Armin Felder committed
}

bool ApplePushModel::sendMessage() {

    std::vector<std::string> tokens;

Armin Felder's avatar
Armin Felder committed
    Json::FastWriter fast;

    tokens.push_back(mDeviceToken);

    PusherContent content;

    if(mPayload.length()){
        content.userData = "\"ejson\":"+mPayload+"";
    }else{
        content.userData = "";
    }
Armin Felder's avatar
Armin Felder committed

    content.badge = mBadge;
    content.content = mApn;
    content.sound = mSound;
Armin Felder's avatar
Armin Felder committed


    mPusher.isSandBox = true;
    mPusher.pushNotification(content, tokens);


    //TODO: shoud be fixed, pusherCPP does not give a return value
    return true;
Armin Felder's avatar
Armin Felder committed
}