Skip to content
New issue

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

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

Already on GitHub? # to your account

firebaseMessaging.sendEachForMulticast: app/network-error #2821

Open
moshOntong-IT opened this issue Dec 25, 2024 · 2 comments
Open

firebaseMessaging.sendEachForMulticast: app/network-error #2821

moshOntong-IT opened this issue Dec 25, 2024 · 2 comments

Comments

@moshOntong-IT
Copy link

export async function pushNotification({
  identifiers,
  body,
  metadata,
  title,
  link,
}: PushMessage) {
  try {
    logger.debug(`Push Notification: ${JSON.stringify(identifiers)}`);

    const userPreIdentifiers = identifiers.users;
    const teamPreIdentifiers = identifiers.teams;

    const userIdentifiers: string[] = [];

    if (userPreIdentifiers === "allUsers") {
      const allUserIdentifiers = await getAllUsersIdentifiers();
      userIdentifiers.push(...allUserIdentifiers);
    } else {
      const userTokens = await getUserIdentifiers({
        userRefs: userPreIdentifiers || [],
      });
      userIdentifiers.push(...(userTokens || []));
    }

    const teamIdentifiers = await getTeamIdentifiers({
      teamRefs: teamPreIdentifiers || [],
    });

    const tokens = [...(userIdentifiers || []), ...(teamIdentifiers || [])];
    logger.debug("All tokens:", tokens);
    if (tokens.length === 0) {
      logger.debug("No tokens found for push notification");
      return;
    }

    const message: MulticastMessage = {
      tokens,
      notification: {
        title,
        body,
      },
      data: metadata as { [key: string]: string },
      ...(link && {
        webpush: {
          fcmOptions: {
            link,
          },
        },
      }),
    };
    const response =
      await firebaseConfig.firebaseMessaging.sendEachForMulticast(message);

    logger.debug(
      `Successfully sent push notification: ${JSON.stringify(response)}`
    );
  } catch (error) {
    logger.error("Error sending push notification:", error);
  }
}
{"responses":[{"success":false,"error":{"code":"app/network-error","message":"Error while making request: Stream closed with error code NGHTTP2_PROTOCOL_ERROR. Error code: ERR_HTTP2_STREAM_ERROR"}}],"successCount":0,"failureCount":1}

I am using the "firebase-admin": "^13.0.2", sdk. The sendEachForMulticast not working on my development mode (did not try yet in production). However, if I am using the send method it will successfully push the notification.

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@lahirumaramba
Copy link
Member

Is your server/development workflow somehow block http2 outgoing requests?
sendEachForMulticast() and sendEach() methods uses HTTP/2 by default and the send() method uses HTTP/1

You can try disabling HTTP/2 in sendEachForMulticast() using the enableLegacyHttpTransport() function and see if that helps.

const messaging = getMessaging(app);
messaging.enableLegacyTransport();
messaging.sendEach(messages);

See more on Messaging.enableLegacyHttpTransport()

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants