Notification

133 views
Skip to first unread message

Nii Darku

unread,
Jan 11, 2020, 12:29:08 PM1/11/20
to DroidScript
Hi, kindly help with a concrete code on DS Notification.

I need my notification to pop up there with a list of items.

Now when I click on an item in the list I want the notification to shift focus to my app and close the notification.

I tried some code you gave me but was inadequate to provide the help I needed.

I even wrote the code in a service but abandoned it all together.

Steve Garman

unread,
Jan 11, 2020, 1:27:07 PM1/11/20
to DroidScript
That's not something you can do with a single DroidScript notification.

Have you considered using multiple notifications, one for each option?

I have no idea who you are addressing when you write "I tried some code you gave me"
It would have been more helpful to provide a link to the previous help.

It would be even more helpful if you posted the code you have tried along with a description of what you expected it to do and what happened that was different from what you expected.

Nii Darko Darkoh

unread,
Jan 12, 2020, 4:13:47 PM1/12/20
to DroidScript
// declared notification object
notify = app.CreateNotification();

      window.onload = function (){ 
    //Start/connect to our service.
    svc = app.CreateService( "this","this", OnServiceReady );
    svc.SetOnMessage( OnServiceMessage );

    //This will cause your service to start at boot.
    //(Set it to "none" if you need to stop it starting)
    app.SetAutoBoot( "Service" );
        }

        //Called after our service has started.
        function OnServiceReady(){
            console.log( "Service Ready" );
        }
    
        //Called when messages comes from our service.
        //this will be triggered by a clk on the notif generated by the service
        function OnServiceMessage( msg ){
         if(msg=="3"){
                alert('FITTA notification recieved');
    //show advert page. 
         } 
        }

        function createAlert(recCount, alertId){ 
       notify.SetMessage("Ticker","MyMessage","You have " + recCount + " Messages from Me");
           notify.SetSmallImage( "Img/notify.png" );
           notify.SetLargeImage( "Img/testNotify.png" );
           notify.Notify(alertId); 
        }

Nii Darko Darkoh

unread,
Jan 12, 2020, 4:48:37 PM1/12/20
to DroidScript
Steve, I had this code from the HTML5 Notification API. When I run it on the site it worked but fails to run when i brought it into my DS app.
It reportted that "browser doent support it" . The code is below..

<!DOCTYPE html>
<html>

    <body>

        <button onclick="notifyMe()">Notify me!</button>

        <script>
        function notifyMe() {
          // Let's check if the browser supports notifications
          if (!("Notification" in window)) {
            alert("This browser does not support desktop notification");
          }

          // Let's check if the user is okay to get some notification
          else if (Notification.permission === "granted") {
            // If it's okay let's create a notification
            var notification = new Notification("Hi there!");
          }

          // Otherwise, we need to ask the user for permission
          // Note, Chrome does not implement the permission static property
          // So we have to check for NOT 'denied' instead of 'default'
          else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {

              // Whatever the user answers, we make sure we store the information
              if(!('permission' in Notification)) {
                Notification.permission = permission;
              }

              // If the user is okay, let's create a notification
              if (permission === "granted") {
                var notification = new Notification("Hi there!");
              }
            });
          }

        }
        </script>

    </body>
</html>


Nii Darku

unread,
Jan 15, 2020, 4:32:42 PM1/15/20
to DroidScript
Hi Steve, below is the code you requested. I really need this code to work. Kindly help out. Thanks.

Steve Garman

unread,
Jan 16, 2020, 8:21:56 AM1/16/20
to DroidScript
1) Why are you using window.onload? If this is a native app you need to use OnStart

2) What code is in your Service.js file?

3) Will you be keeping the foreground app open all the time or will the service be opening it by code?

4) Do you really need a service? If you want to open an app in response to a notification being touched, you can do that directly in a normal app without a service

When I suggested you post the code you had tried, I wrote:

"It would be even more helpful if you posted the code you have tried along with a description of what you expected it to do and what happened that was different from what you expected."

Without that, we can only guess what you are looking for and guessing is just a waste of our time.

Nii Darko Darkoh

unread,
Jan 17, 2020, 11:43:28 AM1/17/20
to DroidScript
Hi Steve, I have the answers to your questions as follows.

1) Why are you using window.onload? If this is a native app you need to use OnStart.
ANS: 
Its an HTML app that's why I use window.onload


2) What code is in your Service.js file?
ANS:
//CALLED WHEN SERVICE IS STARTED.
function OnStart(){ 
app.ShowPopup( "Hello from Service!" ); 
//var notifyad = setInterval( notifyADs, 300000 );
var viewcall = setInterval(sendPCA, 180000); // (PCA=Profile, Calls, Ads) every 3 minutes
//var getads = setInterval(fetchAds, 240000); // every 5 minutes 
//var sndloc = setInterval(sendLocation, 240000); // every 4 minutes
}

function onError(tx, error){
alert(error.message);
}

//CALLED WHEN WE GET A MESSAGE FROM MAIN APP.
function OnMessage( msg ){
console.log( msg );

//Handle commands from main App.
if( msg=="change" ) diff = (diff > 0 ? -1 : 1);
}

//Do some work.
function DoWork(){
//This is where we do some regular background task
//(here we just modify a counter).
count += diff;

//SEND DATA TO THE APP (IF IT IS RUNNING).
app.SendMessage( count );
}


3) Will you be keeping the foreground app open all the time or will the service be opening it by code?
ANS:
I want the service to open it since the app would not be open all the time.


4) Do you really need a service? If you want to open an app in response to a notification being touched, you can do that directly in a normal app without a service
ANS: 
Yes I need a service to trigger the app if its closed. 
If its already running, then it should just show on the foreground.
Also I need the notification to close when its clicked.  

Steve Garman

unread,
Jan 17, 2020, 2:58:11 PM1/17/20
to DroidScript
1) It would be hard for it to look less like an html app but if that's whatbit is and it's using native code
a) does it have a script tag loading "file:///android_asset/app.js" ?
b) does it call app.Start() from its body tag?
c) does it have an OnStart function (even an empty one) for app.Start to call?
d) what does the html do?
e) have you considered using a native app and a WebView instead?

2) Looks like you may be able to do something from that start

3) you can open the main app by calling
app.StartApp() without any parameter

4) that seems like a sensible reason to use a service.
If you continue to only send the notification in response to a message from the service, your notification will close when it is pressed

Once you have some code you would expect to work, I'm sure you will address the following question:
5) what do you expect your code to do? what happens that is different from what you expect?


Nii Darko Darkoh

unread,
Jan 17, 2020, 4:04:42 PM1/17/20
to DroidScript
Hi Steve,
My answers as follow..


1) It would be hard for it to look less like an html app but if that's whatbit is and it's using native code
a) does it have a script tag loading "file:///android_asset/app.js" ?
    ANS: Yes

b) does it call app.Start() from its body tag?
   
c) does it have an OnStart function (even an empty one) for app.Start to call?
    ANS: No, I guess i will need it to enable it to be called when closed..right?


d) what does the html do?
    ANS: It does some interface work.. thats all.. i can testify that the html side of things are okk, because i have built other apps with this same approach.


e) have you considered using a native app and a WebView instead?
    ANS: I must say webview is out of the question totally. Native too wouldn't serve this purpose since i am fluid with the html tags over the native layouts.

2) Looks like you may be able to do something from that start

    ANS: I would definitely consider migrating my windows.Onload code into the OnStart() function.

3) you can open the main app by calling app.StartApp() without any parameter.
    ANS: Thanks that's very helpful.

4) that seems like a sensible reason to use a service.

    ANS: Yeah. I actually discarded the service idea because it appeared to slow my phone and make it hot but later figured it was from my little memory space.
              I will revert to service. In the meantime i am using recurring function calls using the setInterval.
 
If you continue to only send the notification in response to a message from the service, your notification will close when it is pressed.
    ANS: No problem if it closes on click. But my issue is that how do I get some action performed on clicking the notification ?

Once you have some code you would expect to work, I'm sure you will address the following question:

    ANS: how do I get some action performed on clicking the notification ? 

              Where do I put the code. i thought of notificationObject_OnTouch()
              Is there an event for the notificationObject at all ?

5) what do you expect your code to do? what happens that is different from what you expect?

     ANS: I expect my code to
    a. open the app  

    b. display a div in html (I have these areas under control. My issue is where to put the code when the notificationObject is clicked)


Nii Darko Darkoh

unread,
Jan 17, 2020, 4:13:31 PM1/17/20
to DroidScript
Steve, check this out...

I have noticed in DS that you have two sepearate packages of Help codes.
The one with the rocket icon and the other with the book icon. I took the following code from the rocket help since the one in the book is not detailed .

//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create first button.
btn1 = app.CreateButton( "Notify Me", 0.6 );
btn1.SetOnTouch( btn1_OnTouch );
lay.AddChild( btn1 );
//Create second button.
btn2 = app.CreateButton( "Update Me", 0.6 );
btn2.SetMargins( 0, 0.05, 0, 0 );
btn2.SetOnTouch( btn2_OnTouch );
lay.AddChild( btn2 );
//Create third button.
btn3 = app.CreateButton( "Notify Me (with image)", 0.6 );
btn3.SetMargins( 0, 0.05, 0, 0 );
btn3.SetOnTouch( btn3_OnTouch );
lay.AddChild( btn3 );
//Create fourth button.
btn4 = app.CreateButton( "Notify Me (full screen)", 0.6 );
btn4.SetMargins( 0, 0.05, 0, 0 );
btn4.SetOnTouch( btn4_OnTouch );
lay.AddChild( btn4 );
//Create fifth button.
btn5 = app.CreateButton( "Cancel All", 0.6 );
btn5.SetMargins( 0, 0.05, 0, 0 );
btn5.SetOnTouch( btn5_OnTouch );
lay.AddChild( btn5 );
//Add layout to app.
app.AddLayout( lay );
//Create notification objects.
notify1 = app.CreateNotification();
notify2 = app.CreateNotification( "AutoCancel" );
notify3 = app.CreateNotification( "AutoCancel,FullScreen" );
}

//Called when user presses notify button.
//Note: Lights only show when device screen is turned off.
function btn1_OnTouch() 
{
    notify1.SetMessage( "You have a notification!", "My Title", "My Details" );
    notify1.SetSmallImage( "/Sys/Img/Notify.png" );
    notify1.SetLights( "#00ffff", 500, 500 );
notify1.Notify();  
}

//Called when user presses the udate button.
function btn2_OnTouch()
{
    notify1.SetMessage( "An Update!", "Updated Title", "Updated Details" );
notify1.Notify();        
}

//Called when user presses notify with image button.
//Note: The small icon cannot be changed when running with
//the DroidScript IDE, but it will pickup your app icon when
//if you build an APK.
function btn3_OnTouch()
{
    notify2.SetMessage( "You have a notification!", "My Title", "My Details" );
    notify2.SetSmallImage( "/Sys/Img/Notify.png" );
    notify2.SetLargeImage( "/Sys/Img/Hello.png" );
notify2.Notify();        
}

//Called when user presses notify full screen button.
function btn4_OnTouch()
{
    app.ShowPopup( "Press your Home button and wait a few seconds" );
    setTimeout( DelayedNotify, 7000 );
}

//Set (delayed) notification.
function DelayedNotify()
{
    notify3.SetMessage( "Full screen notification!", "My Title", "My Details" );
    notify3.SetSmallImage( "/Sys/Img/Notify.png" );
notify3.Notify();       
}

//Called when user presses cancel button.
function btn5_OnTouch()
{
    notify1.Cancel();
    notify2.Cancel();
    notify3.Cancel();
}

...................................

As you can see there is no code to handle the notification object click.
I am about to experiment with the OnTouch() event.. as follow 

notify1_OnTouch(){
.... some code..
}

Steve Garman

unread,
Jan 17, 2020, 4:24:29 PM1/17/20
to DroidScript
In the docs (left hand side, book icon) there is a description of app.GetNotifyId. You should read it, it includes the following example code.

function OnStart()
{
var id = app.GetNotifyId();

if( id ) app.Alert( id, "Notification ID" );
else
{
not = app.CreateNotification();
not.SetMessage(
"You have an urgent notification",
"Press me!", "Do as the title says."
);
not.Notify( 1234 );
setTimeout( app.Exit, 2000 );
}
}

Steve Garman

unread,
Jan 17, 2020, 5:08:35 PM1/17/20
to DroidScript
You don't have to use GetNotifyId in OnStart.

If you want to check every time a notification was pressed even if the app was already open try


function OnData( isStartUp )
{
   
var id=app.GetNotifyId();
   
if(id == "touchable") OnNotification(id);
}
function OnNotification(id)
{
     app
.ShowPopup( "notification touched" );
     
//remove following line if you want to leave notification on screen
     notify
.Cancel( id );
}It is worth noting that none of this stuff was written fot html apps

It is worth noting that none of this stuff was written fot html apps so all the docs and samples assume a native app.

Nii Darko Darkoh

unread,
Jan 17, 2020, 5:33:55 PM1/17/20
to DroidScript
Steve, i thank you so far for your patience and tolerance.. we are not finished though..haha
The following code your sent is breaking the ice for me... 

function OnData(isStartUp){
    var id=app.GetNotifyId();
    if(id == "touchable") OnNotification(id);
}
function OnNotification(id){
    app.ShowPopup( "notification touched" );
    //remove following line if you want to leave notification on screen
    notify.Cancel(id);
}

Please clarify a few things for me.

1. I guess the OnData() is the function that responds to Notification clicks..right?
2. The app.GetNotifyId() will retrieve the notification id. Can I set this ID to extract and use it for specific purposes?
3. let me guess. the following could be a logical notification flow..

//INSIDE MY SERVICE
    notify = app.CreateNotification();
    notify.Notify("testID");

//INSIDE MY APP
function OnData(isStartUp){
    var id=app.GetNotifyId();
    if(id == "testID") OnNotification(id);

Nii Darku

unread,
Jan 20, 2020, 4:17:08 AM1/20/20
to DroidScript
Thanks Steve for all the help. My notification is working now.
I need a few minor issues sorted out for me.

1. What are the specifications for the notification icons (big and small)?

2. What is the isStartUp in the following code and how do i use it?

Function OnData(isStartUp){
.....
}

Steve Garman

unread,
Jan 20, 2020, 5:03:05 AM1/20/20
to DroidScript
I'm not sure what the official specs are for the notification icons but I can tell you what I use.
Both are png files 512 pixels square. Big is full colour.

Small is one single colour on a transparent background.
That is the combination I find works best on all devices. Samsung ignore the Android rules on colour for displaying the small icon, at least for some versions of Android.

OnData is fired every time your app receives a targeted intent and isStartup is a boolean so you can tell if it may be a blank intent received at startup.

It is not really useful when receiving notifications.
Check if app.GetNotifyId() returns something instead.

Nii Darku

unread,
Jan 20, 2020, 5:10:56 AM1/20/20
to DroidScript
Thank you Steve. You have been very helpful. *****
Reply all
Reply to author
Forward
0 new messages