Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
hyochan opened this issue Jul 23, 2023 · 2 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@hyochan
Copy link

hyochan commented Jul 23, 2023

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();
      }
    }
    ```

2. Deploy the application to Vercel.

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

4. 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

  • I agree to follow this project's Code of Conduct
@hyochan hyochan added the bug Something isn't working label Jul 23, 2023
@nan-li
Copy link
Contributor

nan-li commented May 9, 2024

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"?

@shepherd-l
Copy link

Closing issue due to inactivity. If you are still having issues, please open a new report with updated information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants