Skip to content

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

@himanshuf35

Description

@himanshuf35
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
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions