Skip to content

Missing Equivalent of onTokenRefresh in New Firebase JS SDK #8468

@boussaidev

Description

@boussaidev

Operating System

mac

Environment (if applicable)

latest browsers

Firebase SDK Version

latest

Firebase SDK Product(s)

Messaging

Project Tooling

vuejs with firebase

Detailed Problem Description

I am using the latest version of the Firebase JS SDK and am unable to find an equivalent to the onTokenRefresh method. This method was previously used to listen for changes in the messaging token, which is essential for many use cases.

Expected Behavior:

I would expect the new version of the SDK to provide a similar mechanism for listening to token changes, either through a direct replacement for onTokenRefresh or an alternative approach.

Impact:

The absence of this functionality is hindering my ability to implement features that rely on the messaging token, such as sending personalized notifications or tracking user engagement.

Steps to Reproduce:

Create a new Firebase project.
Initialize the Firebase app in your JavaScript code.
Attempt to use the onTokenRefresh method.

Steps and code to reproduce issue

import { initializeApp } from 'firebase/app';
import { getMessaging, onMessage } from 'firebase/messaging';

const firebaseConfig = {
// Your Firebase project configuration goes here
};

const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);

// Request permission to receive notifications (optional but recommended)
messaging.requestPermission()
.then(() => {
console.log('Notification permission granted.');
})
.catch((error) => {
console.error('An error occurred while requesting permission:', error);
});

// Listen for incoming messages in the foreground
onMessage(messaging, (payload) => {
console.log('Received message in foreground:', payload);

// You can now process the message payload here:
// - Access the notification data: payload.data
// - Access the notification content: payload.notification

// Example: Display notification content in an alert
if (payload.notification) {
const title = payload.notification.title;
const body = payload.notification.body;
alert(${title}: ${body});
}
});

// Optionally listen for token changes (if needed for your use case)
messaging.onTokenRefresh((token) => {
console.log('New token:', token);
// Send the token to your server for further processing
});

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