OneSignal C++ Client SDK is Now Available

We are pleased to announce that the OneSignal C++ Client SDK is now available. This new C++ Client supports developers using OneSignal by facilitating the integration of the OneSignal REST API with backend events, data, and more. This release is part of our ongoing initiative to add server SDKs across various languages and technologies.

C++ remains one of the most popular programming languages in the world and we are happy to support it! Our client SDK is based on the well-established HTTP library cpprestsdk.

What is This Library?

OneSignal CPP API is a faster way to get started using OneSignal REST API that is accessible as a REST service.

This library has been generated using OpenApi Generator project with several modifications and fixes on our side.

Installation

Prerequisites

Install cpprestsdk.

  • Windows: vcpkg install cpprestsdk cpprestsdk:x64-windows boost-uuid boost-uuid:x64-windows
  • Mac:
    brew install cpprestsdk
    brew install openssl
  • Linux: sudo apt-get install libcpprest-dev

Cmake integration

You can cmake --install . and then use find_package(CppRestOneSignalAPIClient REQUIRED).
Alternatively you can have it as a subdirectory as in the example below.

Take a look on our test CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(CppRestOneSignalAPIClientTest)

set(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

# Test dependencies
find_package(Catch2 REQUIRED)

# OneSignal Client Library
add_subdirectory(deps/onesignal)

# Executables for tests
add_executable(tests test.cpp)

target_link_libraries(tests Catch2::Catch2 CppRestOneSignalAPIClient)

Build

cmake -DCMAKE_CXX_FLAGS="-I/usr/local/include -I/usr/local/Cellar/include -I/usr/local/opt/include" \
-DCMAKE_MODULE_LINKER_FLAGS="-L/usr/local/lib -L/usr/local/Cellar/lib -L/usr/local/opt/lib" \
-DCMAKE_EXE_LINKER_FLAGS="-L/usr/local/lib -L/usr/local/Cellar/lib -L/usr/local/opt/lib -L/usr/local/opt/openssl/lib" \
-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl \
-DOPENSSL_LIBRARIES=/usr/local/opt/openssl/lib \
make

Build on Windows with Visual Studio (VS2017)

  • Right click on folder containing source code
  • Select 'Open in visual studio'
  • Once visual studio opens, CMake should show up in top menu bar.
  • Select CMake > Build All.

*Note: If the CMake menu item doesn't show up in Visual Studio, CMake
for Visual Studio must be installed. In this case, open the 'Visual Studio
Installer' application. Select 'modify' Visual Studio 2017. Make sure
'Desktop Development with C++' is installed, and specifically that 'Visual
C++ tools for CMake' is selected in the 'Installation Details' section.

Also be sure to review the CMakeLists.txt file. Edits are likely required.*

How to use

Initializing the OneSignal CPP Client library

#include "CppRestOneSignalAPIClient/ApiClient.h"
#include "CppRestOneSignalAPIClient/ApiConfiguration.h"
#include "CppRestOneSignalAPIClient/api/DefaultApi.h"

using com::onesignal::client::api::ApiClient;
using com::onesignal::client::api::ApiConfiguration;
using com::onesignal::client::api::DefaultApi;

using utility::string_t;
using utility::conversions::to_string_t;

const std::string APP_ID = "<YOUR_APP_ID>";
const std::string APP_KEY_TOKEN = "<YOUR_APP_KEY_TOKEN>";
const std::string USER_KEY_TOKEN = "<YOUR_USER_KEY_TOKEN>";

static DefaultApi * createApi() {
    // Settings up the client
    const auto configuration = ApiClient::getDefaultConfiguration();
    configuration->setAppKeyToken(APP_KEY_TOKEN);
    configuration->setUserKeyToken(USER_KEY_TOKEN);

    const auto apiClient = std::make_shared<ApiClient>(configuration);

    return new DefaultApi(apiClient);
}

Creating a notification model

static std::shared_ptr<Notification> createNotificationModel() {
    const auto notification = std::make_shared<Notification>();
    notification->setAppId(APP_ID);

    const auto content = std::make_shared<StringMap>();
    content->setEn(to_string_t("OneSignal C++ Client Test: Create notification"));
    std::vector<string_t> vect{ to_string_t("Active Users") };

    notification->setContents(content);
    notification->setIncludedSegments(vect);
    notification->setIsAnyWeb(true);
    notification->setIsChrome(true);

    return notification;
}

Sending a notification

const auto api = createApi();

// Creating a notification
const auto notification = createNotificationModel();

// Send a notification
const auto response = api->createNotification(notification);
const auto & responseData = response.get();

// Check the result
CHECK(responseData->getId().size() > 0);
CHECK_FALSE(responseData->errorsIsSet());

You can find more examples in our GitHub repo!

Reference

Visit our REST API Reference documentation for a complete list of our supported endpoints.

Visit our C++ API reference for a list of supported functions and view implementation examples in C++.

Share Your Feedback

If you’ve had a chance to use our new C++ SDK and have any feedback or suggestions you’d like to share, please reach out to us on the GitHub repository or our Discord server.

If you encounter any bugs or technical issues while using our new Java SDK, we want to know so we can make things right. Please be sure to file a report on our GitHub repository.

If you’ve found the plugin helpful, we’d appreciate it if you left us a review on G2.

Join our Discord Server