React-native-push-notification: Crash: JSON value of type NSDate cannot be converted to a date

Created on 29 Apr 2016  路  6Comments  路  Source: zo0r/react-native-push-notification

Hello!

I'm scheduling multiple notifications, but sometimes (that's the worst part; it's just sometimes) it crashes and throws this error:

JSON value '2016-05-25 04:00:00 +0000' of type NSDate cannot be converted to a date

Here is the code used to schedule:

static scheduleNotification(quoteDate, quoteHours, quoteOfDay){
    let notifDate = new Date(quoteDate);
    notifDate.setHours(quoteHours.hour, quoteHours.mins, 0);
    let message = '"'+quoteOfDay.frase + '" - ' + quoteOfDay.autor;
    PushNotification.localNotificationSchedule({
        id: 0,
        message: message,
        date: notifDate
    })
}

I've tried to use "date: new Date(notifDate)", but no lucky.
Any ideas?

Thank you

Edit:

It seems to be related with this issue: https://github.com/facebook/react-native/issues/4049

Most helpful comment

Hey guys, anyone having this problem, it might be solved passing a date in ISO 8601 format. Just use ".toISOString()". That works for me, all the crashes are gone! (:

All 6 comments

Hey guys, anyone having this problem, it might be solved passing a date in ISO 8601 format. Just use ".toISOString()". That works for me, all the crashes are gone! (:

You can also pass the unix timestamp number from .getTime().

Yeah, probably it would work too. I exhaustively tested with ISOString and had no problems, not sure about unix timestamp, but it makes sense.

Can you update the example in the README with this info

None of the approaches here seem to work for a multi-platform usage.

localNotificationSchedule expects date.getTime() to exist, as it would when used as the README describes.

For Android use:

date: new Date(Date.now() + (10 * 1000))

for iOS use:

date: new Date(Date.now() + (10 * 1000)).getTime()

Is there a reason why we are doing this?

Notifications.localNotificationSchedule = function(details: Object) {
    if ( Platform.OS === 'ios' ) {
        this.handler.scheduleLocalNotification({
            fireDate: details.date,
            alertBody: details.message
        });
    } else {
        details.fireDate = details.date.getTime();
        delete details.date;
        this.handler.scheduleLocalNotification(details);
    }
};
Was this page helpful?
0 / 5 - 0 ratings