React-native-push-notification: Pass data to localNotifcation and receive it in onNotification event

Created on 11 Aug 2016  路  9Comments  路  Source: zo0r/react-native-push-notification

Hey there!

I am trying to pass data to a localNotifcation and receive it in onNotification when i have clicked on it:

My code at presents:

     PushNotification.localPushNotification({
        title  : 'Title',
        message: 'Message',
        data   : {
            venueName: venue.name,
            venueType: venue['@type'],
            venueId  : venue['@id'],
            type     : 'venue'
        }
    });


    PushNotification.configure({
        // (optional) Called when Token is generated (iOS and Android)
        onRegister: function (token) {
            console.log('NOTIFICATIONTOKEN:', token.token);
            Helper.registerDevice(token.token);
        },

        // (required) Called when a remote or local notification is opened or received
        onNotification: function (notification) {

                const {dispatch, User} = context.props;
                let type = notification.type;

                if (notification.userInteraction) {
                    switch (notification.type) {
                        case 'venue':
                            alert('Go to Venue View')
                            break;
                    }
                }
        },

        popInitialNotification: true
    });

I can pass the data to my localNotifcation, but when i have clicked on the PushNoti, and debugging onNotification, then there is no data. Simply this is what i get:

screen shot 2016-08-11 at 13 55 46

Any ideas?

Most helpful comment

@BigPun86 Can you try showing the notification like this and check if you get the data in the notification. Essentially, don't wrap it in data

PushNotification.localPushNotification({
    title  : 'Title',
    message: 'Message',
    venueName: venue.name,
    venueType: venue['@type'],
    venueId  : venue['@id'],
    type     : 'venue'
});

I am using it this way and I am able to get the data in notification handler.

All 9 comments

What is the issue that you are facing? Are you not able to access the data in the notification? Can you try console.log the notification to see what you got back?

@varungupta85 The image where displayed shows my notification object within my onNotification function. There is no notifcation.data which i need and which i have passed to my localNotification when created...

@BigPun86 Can you try showing the notification like this and check if you get the data in the notification. Essentially, don't wrap it in data

PushNotification.localPushNotification({
    title  : 'Title',
    message: 'Message',
    venueName: venue.name,
    venueType: venue['@type'],
    venueId  : venue['@id'],
    type     : 'venue'
});

I am using it this way and I am able to get the data in notification handler.

@varungupta85 You are the best. But why this won麓t work when i give him an object?

@BigPun86 Thanks :) I think it is not working with an object due to the way the object is converted to ReadableMap and then back to JS object in the native bridge code. I think the conversion to ReadableMap is not able to handle nested object but that is just my guess.

Hi is this topic about android or IOS? Is there a way to pass data on android? Thanks

@mcmatan It was an Android issue

@BigPun86 Thanks!

Closing due to inactivity. Please reopen if this is still an issue for you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nguyenvanphuc2203 picture nguyenvanphuc2203  路  3Comments

nidzovito picture nidzovito  路  3Comments

DanDance picture DanDance  路  3Comments

atteia picture atteia  路  4Comments

Benzer1406 picture Benzer1406  路  3Comments