0

I want to know how to send a local notification in worklight. I want the notification to show in the device screen even when the application is not in the foreground. I have searched and only found using WL.SimpleDialog, however this only shows when the user opens the application. Is there a way to do this?

AWSSET
  • 397
  • 1
  • 4
  • 12

1 Answers1

1

If you are in fact referring to Location Notifications, see these questions:

If when you say "alert" you actually mean a dialog...

  • You can use Cordova's pause event, like the below. Displaying an alert() is not professional looking IMO. Anyway, you can replace the below WL.SimpleDialog with an alert...

    common\js\main.js

    function wlCommonInit(){
        document.addEventListener("pause", showDialog, false);
    }
    
    function showDialog() {
        WL.SimpleDialog.show(
            "My Dialog", "My Text", 
            [{text: "My Button", handler: function() { }}]
        );
    }
    

    There is no "rendering" when the application is not in the foreground, so I think the above is the best you're going to get.
    When sending the application to the background, display an alert. This way when you then bring the application back to the foreground, a dialog will be displayed.

Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • SimpleDialog will send an alert, but user can only see it when he/she opens the application again. There are some instances that i want my application to automatically send alerts. So I would want the user to receive it even when the application is in the background. I think it is called local notification. – AWSSET Sep 05 '14 at 11:45
  • Then don't write "alert" when you actually mean a notification. context matters. :) See my updated answer. – Idan Adar Sep 05 '14 at 11:52
  • Hi. Sorry for the late reply. I followed the steps you provided in this [link](http://stackoverflow.com/questions/24077230/using-katzer-local-notification-in-ibm-worklight). However, I was not able to have the generated cordova_plugins.js you've mentioned. Is this automatically generated? how? – AWSSET Sep 09 '14 at 10:25
  • cordova_plugins.js is generated by Worklight and is available AFTER you build the project -- in the native project. – Idan Adar Sep 09 '14 at 10:25
  • yes, I have built the project, and the files is still not generated. – AWSSET Sep 09 '14 at 10:30
  • 1
    Yes, it is. For example, android_project\assets\www\default\worklight\cordova_plugins.js. Run a search.... – Idan Adar Sep 09 '14 at 10:32
  • Hi, sorry. i have another problem. I included all the necessary files written in your post, but i am still encountering a problem: Cannot read property 'notification' of undefined. Have i missed anything? I have added the local-notification.js and have included it in the html, also imported the java files, updated the cordova_plugin.js and config.xml. – AWSSET Sep 09 '14 at 14:53