OneSignal / onesignal-node-api

OneSignal Node Client
Other
28 stars 12 forks source link

[Bug]: OneSignal integration failing intermittently when deployed on Vercel #63

Open hyochan opened 11 months ago

hyochan commented 11 months ago

What happened?

I'm integrating OneSignal for push notifications in my Next.js application. Everything works perfectly in the local environment. However, after deploying to Vercel, the notifications fail intermittently, especially on the first attempt.

Steps to reproduce?

1. [Clone the repository](https://github.com/prisma-korea/prisma-nextjs-rsc) and install `"@onesignal/node-onesignal": "^2.0.1-beta1"`. Set up the necessary environment variables (ONESIGNAL_APP_ID, ONESIGNAL_REST_API_KEY).

2. Create serverless function

    ```ts
    import * as OneSignal from '@onesignal/node-onesignal';
    import type {NextApiRequest, NextApiResponse} from 'next';

    type Reply =
      | {
          key: string;
          message: string;
        }
      | {id: string; description?: string | null};

    export default async function handler(
      req: NextApiRequest,
      res: NextApiResponse<Reply>,
    ): Promise<void> {
      const {body, method} = req;

      const createNotification = async (
        notification: Omit<OneSignal.Notification, 'app_id'>,
      ): Promise<OneSignal.CreateNotificationSuccessResponse | undefined> => {
        const {ONESIGNAL_APP_ID, ONESIGNAL_REST_API_KEY} = process.env;

        try {
          const configuration = OneSignal.createConfiguration({
            authMethods: {
              app_key: {
                tokenProvider: {
                  getToken() {
                    return ONESIGNAL_REST_API_KEY as string;
                  },
                },
              },
            },
          });

          const client = new OneSignal.DefaultApi(configuration);

          return client.createNotification({
            ...notification,
            app_id: ONESIGNAL_APP_ID as string,
          });
        } catch (e) {
          // eslint-disable-next-line no-console
          console.log(e);
        }
      };

      switch (method) {
        case 'POST':
          const userId = <string>body.userId;
          const message = <string>body.message;
          const data = <Record<string, string>>body.data;

          if (!userId || !message) {
            res.status(500).send({
              key: 'BAD_REQUEST',
              message: 'userId and message are required',
            });

            return;
          }

          const notification: Parameters<typeof createNotification>[0] = {
            thread_id: 'admin',
            android_group: 'admin',
            include_external_user_ids: [userId],
            contents: {en: message},
            data,
          };

          createNotification(notification);

          res.status(200).send({
            key: 'SUCCESS',
            message: 'Successfully sent notification',
          });
          break;
        default:
          res.status(404).end();
      }
    }
  1. Deploy the application to Vercel.

  2. Try sending a notification through the provided endpoint in the deployed application.

  3. Observe that it often fails on the first attempt and works intermittently after that.

What did you expect to happen?

I expected the OneSignal notifications to work seamlessly and consistently when deployed on Vercel, just as they do in the local environment.

Relevant log output

Screenshot 2023-07-23 at 11 13 51 PM

Code of Conduct

nan-li commented 1 month ago

Hi @hyochan I apologize for the delay in response. If this issue is still relevant, can you share more what you mean by "4. Observe that it often fails on the first attempt and works intermittently after that"?