Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can test firebase notification OnTokenRefresh method call in javascript?

I have created one firebase notification Web application.When I login from web application then I generate FCM token and send it to my API server this is working properly.I have written onTokenRefresh method in my code but I am not able to test this code. Is there any way to test this code?

firebase.initializeApp(config);
messaging = firebase.messaging();


// Get Instance ID token. Initially, this makes a network call, once retrieved
      // subsequent calls to getToken will return from cache.
      messaging.requestPermission().then(function() {
        return messaging.getToken();
    }).then(function(currentToken) {
        if (currentToken) {
          sendTokenToServer(currentToken);
          updateUIForPushEnabled(currentToken);
        } else {
          // Show permission request.
          console.log('No Instance ID token available. Request permission to generate one.');
          // Show permission UI.
          updateUIForPushPermissionRequired();
          setTokenSentToServer(false);
        }
      })
      .catch(function(err) {
        console.log('An error occurred while retrieving token. ', err);
        showToken('Error retrieving Instance ID token. ', err);
        setTokenSentToServer(false);
      });
    }

     // Callback fired if Instance ID token is updated.
        messaging.onTokenRefresh(function() {
          messaging.getToken()
          .then(function(refreshedToken) {
            console.log('Token refreshed.');
            // Indicate that the new Instance ID token has not yet been sent to the
            // app server.
            setTokenSentToServer(false);
            // Send Instance ID token to app server.
            sendTokenToServer(refreshedToken);
            // ...
          })
          .catch(function(err) {
            console.log('Unable to retrieve refreshed token ', err);
            showToken('Unable to retrieve refreshed token ', err);
          });
        });
like image 458
Rohit Shelhalkar Avatar asked Feb 09 '17 12:02

Rohit Shelhalkar


People also ask

How do I get Firebase to push notifications?

Go to Firebase console — →Project Settings — →Cloud Messaging. To send the message select Body — →Raw — →JSON(application/json). You can send Notification Payload , Data Payload and even both using POSTMAN service.


1 Answers

At the time of writing onTokenRefresh is not used.

In the future the library will change so that tokens can be expired and refreshed by FCM and this callback will be used to signal the change.

The best advice I can give is to "fake this event" by writing a test where you call getToken(), unregister the firebase messaging service worker (This will make the token invalid by unsubscribing the the PushSubscription):

navigator.serviceWorker.getRegistrations().then((regs) => {
  return Promise.all(regs.map(reg => reg.unregister()));
});

Then call the code you'd run in onTokenRefresh code manually.

like image 119
Matt Gaunt Avatar answered Sep 19 '22 18:09

Matt Gaunt