firebase/firebase-js-sdk

pushManager.subscribe() function doesn't return anything, promise is always kept in pending state for registration on new devices.

Closed this issue · 7 comments

Image

Operating System

macOS 15.3.1

Environment (if applicable)

Chromium 139, NW js 102

Firebase SDK Version

12.4.0

Firebase SDK Product(s)

Messaging

Project Tooling

MacOS app with with nw js and react

Detailed Problem Description

getToken is hanging forever and I can't register any more FCM tokens. after going through call trace it is found that is not moving beyond swRegistration.pushManager.subscribe call, Promise returned by it neither resolving nor rejecting(always in pending state).

Note: It is only happening for new device registration.

getToken -> getTokenInternal -> getPushSubscription -> swRegistration.pushManager.getSubscription

At the time of last function execution, swRegistration is also in active state

Image

Steps and code to reproduce issue

import { messaging } from './firebase'
import { getToken, onMessage } from 'firebase/messaging'
import { deviceApi } from '@/store/apis/deviceApi'

const VAPID_KEY =
  'xyz'

// Checks if `Notification` is available
const isNotificationSupported = () =>
  typeof window !== 'undefined' && 'Notification' in window

export async function requestNotificationsPermissions(
  uid: string
): Promise<boolean> {
  console.log('Requesting notifications permission...')
  if (!isNotificationSupported()) {
    console.error('Notifications are not supported in this environment.')
    return false
  }
  const permission = await Notification.requestPermission()

  if (permission === 'granted') {
    console.log('Notification permission granted.')
    await saveMessagingDeviceToken(uid)
    return true
  } else {
    console.log('Unable to get permission to notify.')
    return false
  }
}

// Saves the messaging device token to our backend database via API
async function saveMessagingDeviceToken(uid: string) {
  console.log('Saving messaging device token...')

  try {
    const msg = await messaging()
    if (!msg) {
      console.error(
        'Firebase messaging is not supported or failed to initialize'
      )
      return
    }

    console.log('got msg')

    const fcmToken = await getToken(msg, { vapidKey: VAPID_KEY })
    console.log('finished getToken')
    if (fcmToken) {
      console.log('Got FCM device token:', fcmToken)

      // Save device token using deviceApi
      await deviceApi.registerToken(uid, fcmToken, 'web')

      // Set up foreground message handler
      onMessage(msg, (message) => {
        console.log(
          'New foreground notification from Firebase Messaging!',
          message.notification
        )
        if (isNotificationSupported() && message.notification?.title) {
          new Notification(message.notification.title, {
            body: message.notification?.body || '',
          })
        }
      })
    } else {
      // Need to request permissions to show notifications.
      requestNotificationsPermissions(uid)
    }
  } catch (error) {
    console.error('Unable to save messaging token.', error)
  }
}

export function getNotificationPermissionStatus(): NotificationPermission | null {
  if (!isNotificationSupported()) {
    console.error('Notifications are not supported in this environment.')
    return null
  }
  return Notification.permission
}

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

Any update on this, It is really impacting our application functionality.

Hi @himanshuf35, from "I can't register any more FCM tokens" it sounds like you were able to register tokens, but this no longer works. Can you confirm this?

If this was working in the past, when did you begin observing this issue? Was it after bumping the SDK version?

It stopped working a week ago, even on older builds.
The sdk upgrade changes were done just to check if something has changed from sdk implementation side. I have also tried earlier sdk versions as well.

It can be seen from logs attached that fcm registration api is no longer responding.

I have already followed all the troubleshoot guidelines.

@himanshuf35 Based on your response, it sounds like you're experiencing a service issue. Issues in this repository should be only for the Firebase JavaScript SDK, not the backend services. Please reach out to Firebase Support.

If this turns out to be a service issue that we can mitigate from the client SDKs in some way, please let us know.

Hey @himanshuf35. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

Since there haven't been any recent updates here, I am going to close this issue.

@himanshuf35 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.