Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 513
Jean R. Avatar asked Mar 24 '18 15:03

Jean R.


1 Answers

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>)
like image 127
Jean R. Avatar answered Oct 15 '22 16:10

Jean R.