👁 itoo Push Notification Examples for ALL!

I am pleased to provide below a few examples of how you can use the excellent and remarkable itoo extension by @Kumaraswamy to generate push notifications for your apps, using dataChanged or timer events.

These examples have all been tested on Android 13 (Google Pixel 4a) as compiled apps, and use the sky 4.2 version of the itoo extension, dated 2024-07-05, and version 2 of the Notification Style extension, dated 2024-06-04.

Each example sticks very much to the basics, sending simple text notifications, and without any graphical enhancements, I will leave that up to your creativity. The examples are intended to show you how the extensions work together to generate the push notifications in your app.

  • All of these examples request the Notifications Permission on first run, you MUST ALLOW this permission, otherwise the app will not work as intended.

  • DataChanged events will generate a notification when the message is changed..., for the timer events, a notification is generated when the message is changed and the timer fires. I have the timer set to 15 seconds (15000), this is for testing... you may only want this to fire once every 24 hours (86400000) in production ?

  • None of my examples make use of any MIT provided data servers (e.g. cloudDB or Firebase)

  • In all of the examples I have included the "admin" blocks and components for setting a new/different message. In practice you would have this in a different app, but it makes it much easier to test this way.

I would like to extend my thanks to @Kumaraswamy for his help and support in getting all these examples together.

8 Likes

EXAMPLE 1 [updated for Android 14]

Firebase using own project and the dataChanged event

Prerequisites:

  • a project setup on Firebase
  • in that project a node(tag), at top level, called itoo

image

  • secure rules for that node set to read:true / write:true
{
  "rules": {
    "SECURE": {
       "$uid": {
         ".read": "$uid === auth.uid",
         ".write": "$uid === auth.uid"
       }
     },
    "itoo": {
      ".read": true,
      ".write": true
    },
  }
}

Setup:

  • with the example aia, you will need to add your firebase project configuration data in both the designer for the firebase component, and in the blocks, and ensure the ProjectBucket is set to itoo. (This is required for the "admin" message changing to work)

  • in the blocks, please do not change the value for code in the ExecuteExternalScript method. Code "1" is for Firebase

  • in the execution script blocks, add itoo to the end of your firebase url:
    https://xyz-123-default-rtdb.firebaseio.com/itoo

Blocks

AIA (blank)

FBDataChanged_blank_2.aia (117.4 KB)

1 Like

EXAMPLE 2

Firebase using own project, the Web component and a timer

Prerequisites:

  • a project setup on Firebase
  • in that project a node(tag), at top level, called itoo

image

  • secure rules for that node set to read:true / write:true

image
(note: I have another node setup with secure rules, this stops the firebase people bugging you about your project being insecure)

Setup:

  • with the example aia, you will need to add your firebase project url in the blocks, in two places.

Blocks

AIA (blank)

FBMessage_iToo_blank.aia (115.9 KB)

1 Like

EXAMPLE 3

CloudDB using own redis server (!important)

Prerequisites:

  • your own redis server up and running with SSL, and configured to work with the CloudDB component. See this guide on how you can do that.
  • a CloudDB component for the admin blocks

Setup:

  • enter your redis server details in the CloudDB properties in the designer (required for setting a message)

  • enter your redis server details in the blocks where indicated

  • please do not change the value for code. Code "2" is for CloudDB

Blocks

AIA

ItooCloudDB_blank.aia (115.2 KB)

Addendum

Setting up your own redis server may be somewhat onerous, if you just want to test this method out. Therefore if you do just want to test, you can setup the blocks to use the MIT provided redis server. If that is the case, in the Designer, set the RedisServer to DEFAULT, the Token will magically appear. In the blocks, replace the <YOUR REDIS SERVER ADDRESS> content with clouddb.appinventor.mit.edu, and the YOUR REDIS SERVER PASSWORD with the Token from the designer

DISCLAIMER: You should only use the MIT redis server for demo/testing purposes. Excessive or heavy use may result in your app/s being blocked. There is also no guarantee of data retention, the server data may be cleared without notice.

1 Like

EXAMPLE 4

Google Sheet and Google Form using a timer

Prerequisites

  • A google account :wink:
  • Create a simple Google Form with just one question
  • Create a Google Sheet to receive the form responses
    image
  • In your Google Sheet create a new worksheet, that using a clever formula, will return the last form response value:

    =INDEX(FormResponses1!B1:B,ARRAYFORMULA(MAX(ROW(FormResponses1!B1:B)*(--(FormResponses1!B1:B<>"")))))
    (If you want to see how the formula is constructed see this excellent youtube video)
  • Get the Sheet ID, The worksheet GID for the latest response, and the Form ID
  • Run the Pre-filled link for the Form to get the "entry" number.

Setup

  • in your blank aia project, enter the various IDs in the correct places. Do not use variable blocks.
  • enter the entry number in the position provided
  • use the admin blocks to send a new message via the form to the spreadsheet
  • the process will then poll the spreadsheet for the latest value and return it as a notification

Blocks

AIA

GSMessageiToo_blank.aia (115.8 KB)

EXAMPLE 5

PHP and http server using a timer

Prerequisites:

  • you will need an online http server, running php
  • create two php files, one to create a message, one to get the message

setMsg.php

<?php

$message = $_REQUEST['message'];

$file = '/tmp/itoofile';
$content = json_encode($message);
file_put_contents($file, $content);
$content = json_decode(file_get_contents($file), TRUE);
echo $content

?>

getMsg.php

<?php

$file = '/tmp/itoofile';
$content = json_decode(file_get_contents($file), TRUE);
echo $content

?>

The setMsg php places your message in a file on the server. The getMsg php retrieves the message from the file.

Setup:

  • enter your http server url in the two positions indicated in the block
  • using the admin blocks, setup a message on your server

Blocks

AIA

PHPMessage_iToo_blank.aia (115.6 KB)

EXAMPLE 6

PHP & MYSQL using a timer

Prerequisites:

  • An online http server set up with php and a mysql database
  • create a simple table in a new or existing database
    image
  • create two php files, one for setting the other for getting the message

setitoomessage.php

<?php
    
     require '../creds.php';

     $message = $_REQUEST["message"];

     $conn = mysqli_connect($servername, $username, $password, $dbname);

     if (!$conn) {
     die("Connection failed: " . mysqli_connect_error());
     }

     $sql = "UPDATE itooMessage SET message='$message' WHERE id=1";

     if (mysqli_query($conn, $sql)) {
       echo "Record updated successfully";
     } else {
       echo "Error updating record: " . mysqli_error($conn);
     }

    mysqli_close($conn);
?>

getitoomessage.php

<?php
    
     require '../creds.php';

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM itooMessage WHERE id=1";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
      echo $row["message"];
    }
    } else {
      echo "0 results";
    }

    mysqli_close($conn);
?>

you will note I have my db credentials stored in a separate file...

SETUP

  • enter the http server url in the two positions where it is required in the blocks
  • create a message using the admin blocks

Blocks

AIA

PHPMYSQLMessage_iToo_blank.aia (115.6 KB)

SUMMARY

Well, there you are, six different ways to get push notifications running in your app, at relatively no cost.

I am slightly uncomfortable about the timer approach, because this is always calling data back to be tested in the background process, there are polling routines for php, and triggers for mysql, but integrating these would be a headache, let us keep it simple. I also considered a version for going direct to my redis server using webdis (SEE HERE), but the setup is onerous, and again, let us keep it simple. If anyone has a better solution, I am all ears.

Which one is best ?

This will all depend on your circumstances, what you may have already available, and what you consider to be free (free as in beer...). The Google Sheet/Form approach seems best on this front, but if you have your own online server, then this may be better. It will all come down to what works for you.

I ran a test on Firebase, after leaving the app running for @ 12 hours, and checked the data usage. Calculating it out, it seems that your app could make @ 1 million calls before reaching the 360mb free limit per project, so one call per user per day = 1 million users. Should be enough?

Battery usage on device?

Lots of discussion about this. I am yet to see any significant change to my device battery levels as a result of running this service, either with dataChanged or with a timer.

Support

I have assumed that you have sufficient knowledge with regarding to Google/php/mysql/Firebase to get these examples working, most questions about these web technologies will probably be off-topic, any questions therefore should be about the specifics of using these examples.

Updates
The itoo and notificationStyle extensions have now been updated for Android 14 users / API 34.

If any other such issues arise, then please report here or in the respective topics.

1 Like

Thank you for the fantastic work. This has given me an idea to address the issues Android 14 users face when using iToo. We could use an Alarm Manager to periodically check for updates in the Google Sheet throughout the day and if at a certain alarm an update is available, the system would send a notification. This setup allows you to continue using Alarm Manager as usual, simply by adding a new Alarm Manager item.

1 Like

Thanks alot for your great efforts
The app crashes when i use timer only when i changed the target sdk to 34
Tested in android 14 Samsung A04s

Ensure you update your itoo and notification style extensions to the very latest version that work with API 34 and android 14. Remember, that you need to compile, this will not work with companion.

For now, use this update v4.3:

2 Likes

Example 1: App closes immediately. Device running on Android 11 and itoo version 4.3.1

Did you use the latest NotificationStyle extension as well ?

Yes
Screenshot 2024-09-16 at 4.04.24 PM
Screenshot 2024-09-16 at 4.04.16 PM

Did you use a correct url and api key?

To find out what is going on use logcat

Taifun

Yes, @Taifun. I have filled all the information. And also I will soon be debugging.

I don't get a crash, but I also do not get any response. Tested Android 12 and 14.

[EDIT - oops! forgot to build - will test again] :upside_down_face: