zeyneloz / onesignal-node

A Node.js Library for OneSignal push notification service
MIT License
207 stars 48 forks source link

Schedule Notifications may not be scheduled so far in the future #32

Closed yusuftor closed 4 years ago

yusuftor commented 4 years ago

If I try to schedule a notification in late December or in 2020, I get the error "Schedule Notifications may not be scheduled so far in the future". Why?

This is my code:

export function scheduleNotification(
  { userId, heading, body, time, metadata, onError, onSuccess }:
    {
      userId: string,
      heading: string,
      body: string,
      time: Date,
      metadata?: any,
      onError: (err: string) => void,
      onSuccess?: (id: string) => void,
    },
) {
  const notification = new OneSignal.Notification({
    contents: {
      en: body,
    },
  });

  notification.postBody.data = metadata;
  notification.postBody.headings = { en: heading };
  notification.postBody.send_after = time;
  notification.postBody.filters = [{
    field: "tag",
    key: "user_id",
    relation: "=",
    value: userId,
  }];

  return oneSignalClient.sendNotification(notification, (error: any, _: any, data: any) => {
    if (error) {
      return onError(error);
    } else {
      if (data.id) {
        return onSuccess ? onSuccess(data.id) : null;
      } else {
        if (data.errors.length !== 0) {
          return onError(data.errors[0]); // <- Error occurs here! Not in the error section above
        }
        return null;
      }
    }
  });
}
zeyneloz commented 4 years ago

This is not a library related issue, it is coming from One Signal itself and the error is pretty self explanatory . I would suggest checking One Signal documentation.

yusuftor commented 4 years ago

Yes it turns out it is an error from onesignal but why is the error not appearing in the error part of the callback?