GUI-O Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Episode 21: GUI-O push notification from any device

    Scheduled Pinned Locked Moved
    GUI-O Application Tutorial Series
    1
    1
    966
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kl3m3n
      last edited by kl3m3n

      This example shows how to create custom push notification on any Internet enabled device (WiFi, Ethernet) and send it to a targeted Android device.

      NOTE: Push notifications are available in GUI-O PRO version only.

      The code from this example can be combined with other GUI-O examples using MQTT, Classic Bluetooth or Low Energy Bluetooth. See https://www.gui-o.com/examples/gui-o-and-boards.

      Software prerequisites:

      • Arduino IDE (https://www.arduino.cc/en/software)

      • Arduino source code (you can copy the source code from the snippet below, or download it here

      /*
       * GUI-O Basic Push notification example
       *
       * Copyright (C) 2024, GUI-O Team
       *
       * SPDX-License-Identifier: BSD-3-Clause
       */
      
      
      #include <WiFiClientSecure.h>
      #include <HTTPClient.h>
      #include <ArduinoJson.h>
      
      static const char* ssid = "<ssid>"; // router name
      static const char* pass = "<pass>"; // router password
      static const char* appUuid = "<appUuid>"; // application uuid (retrieve from GUI-O app)
      static const char* resource = "https://apis.gui-o.com:9501/gui-o/resources/api/sendPushNotification"; // request resource / endpoint
      
      static const char* letsencrypt_ca = \
      "-----BEGIN CERTIFICATE-----\n" \
      "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\n" \
      "-----END CERTIFICATE-----\n";
      
      // Connection and requests handling
      WiFiClientSecure client;
      HTTPClient http;
      
      // Icoming data buffer
      String in;
      
      void setup() {
        // Debug output
        Serial.begin(115200);
        
        // Connect WiFi (keep trying...)
        Serial.print("Connecting to ");
        Serial.println(ssid);
        
        // Start WiFi
        WiFi.begin(ssid, pass);
        while(WiFi.status() != WL_CONNECTED) { 
          Serial.print(".");
          delay(500); 
        }
      
        // Set root CA
        client.setCACert(letsencrypt_ca);
        
        Serial.println("WiFi connected!");
      }
      
      void loop() {
        while(Serial.available()) {
          const char c = Serial.read();
          in += c;
        
          if(c == '\n') {
            // Send push notification
            sendPushNotification("GUI-O push notification example", in.c_str(), appUuid);
            // clear buffer
            in = "";
          }  
        }  
      }
      
      void sendPushNotification(const char* title, const char* message, const char* appUuid) {
        DynamicJsonDocument jsonPayload(512);
        jsonPayload["appUuid"] = String(appUuid);
        jsonPayload["messageTitle"] = String(title);
        jsonPayload["messageText"] = String(message);
      
        String payload;
        serializeJson(jsonPayload, payload);
      
        if (http.begin(client, resource)) {
            http.addHeader("Content-Type", "application/json");
      
            const int httpCode = http.POST(payload);
      
            if (httpCode == HTTP_CODE_OK) {
              String response = http.getString();
              Serial.print("Response: ");
              Serial.println(response);
            } 
            else {
              Serial.print("Request failed. Status code: ");
              Serial.println(httpCode);
            }
      
            http.end();
        }
      }
      
      • GUI-O application (https://play.google.com/store/apps/details?id=com.guio.guioapp)

      Hardware prerequisites:

      • Any Internet enabled board (WiFi, Ethernet) that is compatible with Arduino IDE

      1. Retrieve application's universally unique identifier (UUID)

      • Open GUI-O application, navigate to settings and select "Info"

      • Verify that the text "Available" is displayed under "Push notifications" entry

      • Make a note of "Application UUID" (you can share the "Application UUID" by tapping on it)

      2. Modify and upload the code

      • Open Arduino IDE

      • Select "File -> Open"

      • Navigate to location where BasicPushNotification.zip archive was extracted (if downloaded)

      • Select BasicPushNotification.ino and confirm

      • Set your router name and password

      • Set your "Application UUID" (retrieved in form the GUI-O app)

      • Make sure that your board is connected to your PC

      • Ensure that the correct port and baud rate are selected

      • Press upload button to start code upload

      3. Send a push notification

      • Open any serial terminal and connect to your board (baud rate 115200)

      • Enter your push notification message on the serial terminal - the text must be terminated with the \n (new line) character.

      • Observe the response on the serial terminal - if successful, message "Push notification sent" will be displayed

      • Check the push notification on your Android device

      NOTE: Customize the sound, vibration, LED light, and priority of GUI-O app notifications by navigating to "Android settings -> Apps -> Manage apps".


      If you have any questions or run into any problems, please let me know!

      Best regards,
      kl3m3n

      1 Reply Last reply Reply Quote 0
      • First post
        Last post