Search code examples
cordovaibm-mobilefirst

How to get data from notifications in hybrid app using Worklight and Cordova plugin


I am writing hybrid application with IBM MobileFirst 8.5 and Cordova plugin and I don't know how to get data from Notifications.

I want to create a list containing data from notifications within my app. Is it possible using Cordova or IBM MobileFirst? I remember it is possible when I used e.g. Java on Android platform using handler.

All tutorials show only "how to push notification" from my app but I didn't find tutorial "how to get data from notification within my app".


Solution

  • There is no such release "MobileFirst 8.5". There's either 6.3 or 7.0...

    Additionally, you do not send notifications from your app, you only receive them in your app. All tutorials show you how to handle (display) notifications in your app. The sample apps that accompany the tutorials put the notification's payload (the text that was sent via the notification) in an alert - but you could just as well do anything else you'd like with it.

    So for example, in the Hybrid sample app there is the following code:

    // Handle received notification
    function pushNotificationReceived(props, payload) {
        alert("pushNotificationReceived invoked");
        alert("props :: " + JSON.stringify(props));
        alert("payload :: " + JSON.stringify(payload));
    }
    

    Instead of displaying the notification's props and payload in an alert simply take the content and put it in a table...

    Assuming you have a table in your index.html by now:

    <table id="myTable">
    </table>
    

    Then in main.js find it and insert content into it:

    function pushNotificationReceived(props, payload) {
        $("#myTable").html(
         "<tr>" + JSON.stringify(payload) + " " + JSON.stringify(props) + "</tr>");
    }
    

    This is just a very simple abstract of what you could/should do in order to display the push contents inside a table upon handling it. You'll need to further enhance it.