Search code examples
javascriptjsoncordovaphonegaponesignal

Get additionalData from Onesignal (Phonegap)


I read the doc here : https://documentation.onesignal.com/docs/cordova-sdk but it's totally not clear !

I try severals test nothing, I event test to get the title but still nothing

document.addEventListener('deviceready', function () {
  // Enable to debug issues.
  // window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});

  var notificationOpenedCallback = function(jsonData) {
    alert('notificationCallback: ' + JSON.stringify(jsonData)); => json data

        alert('Title : '+ JSON.stringify(jsonData.payload.title)); => nothing

        alert('Title2 : '+ jsonData.payload.title); => nothing


        alert('Additional data: '+ jsonData.payload.additionalData); => nothing
    };


  window.plugins.OneSignal
    .startInit("MY_ID")
    .handleNotificationOpened(notificationOpenedCallback)
    .endInit();
}, false);

How to retrieve this data..

Thanks


Solution

  • After multiple debugs on my app, I finally found the application. The JSON structure of jsonData is :

    jsonData
        notification: {
            payload: {
                title: "YOUR_TITLE",
                body: "BODY",
                additionalData: {
                    "YOUR_KEY" : "YOUR_VALUE"
                },
    

    So to retrieve your data :

    JSON.stringify(jsonData.notification.payload.additionalData.<YOUR_KEY>)