question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting serviceWorkerRegistration and vapidKey after initialisation of AngularFireMessaging

See original GitHub issue

Version info

Angular: 12.2.12

Firebase: 9.6.5

AngularFire: 7.2.0

Background: Messaging.useServiceWorker() and Messaging.useVapidKey() have been deprecated in Firebase 7 and were moved to Messaging.getToken({serviceWorkerRegistration, vapidKey}) (https://firebase.google.com/support/release-notes/js#cloud-messaging_8)

Problem: AngularFireMessaging.getToken is an obervable and the serviceWorkerRegistration and vapid can only be set in the constructor.

Is there a way to set these values after AngularFireMessaging is initialized?

Or is it an option to rewrite AngularFireMessaging.getToken so that it accepts these parameters?

Or can serviceWorkerRegistration and vapidKey be exposed as setters? (see this sample code: https://github.com/kromakollision/angularfire/commit/4f1f7ec4a6a7e854151d29f75970cb260c6752e1)

The use case is registering a custom service worker depending on the app context.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
Sapythoncommented, Mar 9, 2022

It is very easy to see this services file Warning I am not giving you the toast notification code of angular material because it is irrelevant here. So remove those present toast functions.

And please install angular/PWA first here Initialize in app.module.ts

imports[
     provideMessaging(() => getMessaging()),
]

navigator.serviceWorker.getRegistration().then(reg => {
      getToken(this.messaging, {
        vapidKey:
          'BLIMFSWgXuuWE7r2LDUGDXDI6GvKd6Hgl-TXiiDpVNCH3q_2Ia1bsg6sDaKc_Rs_YF5DoRGeqEQoFlRAqBQn358',
          serviceWorkerRegistration:reg
      }).then((token) => {
        console.log(token);
        if(token){
          console.log('Token available')
          onMessage(this.messaging,(payload)=>{
            console.log('Messaging ',payload);
          });
        } else {
          Notification.requestPermission().then((permission) => {
            console.log(permission);  
            if (permission === 'granted') {
              console.log('Permission granted');
            } else {
              console.log('Permission denied');
            }
          })
        }
      }).catch((err) => {
        console.log(err);
      });
0reactions
kartercscommented, Aug 23, 2022

Up on this one the given responses doesn’t fix the problem.

Why do we now need @angular/pwa for this?

the “provideMessaging(() => getMessaging()),” in the import section of app.module gives only errors

and the code provided above doesn’t work either because it can’t find neither getToken nor onMessage.

The deprecation of the “useServiceWorker” is more than 2 years old now, how can we do this please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Messaging | JavaScript SDK | Firebase JavaScript API reference
ServiceWorkerRegistration ; vapidKey ?: string } ) : Promise < string > ... and import a VAPID key for your project with Configure...
Read more >
Angular Push Notifications: a Complete Step-by-Step Guide
Learn how to setup Web Push Notifications in your Angular Application ... We can then generate a VAPID key pair with the following...
Read more >
Initialize Firebase Config inside the service worker file in ...
I am working on Firebase Cloud Messaging on React Js. My project works if I keep my service worker inside the public folder....
Read more >
Module angularfiremessaging has no exported member ...
getRegistration(); const messaging = this.firebase.messaging(); console.log('MESSAGING SW', serviceWorkerRegistration); messaging.getToken({ vapidKey: ' ...
Read more >
angularfire - bytemeta
Setting serviceWorkerRegistration and vapidKey after initialisation of AngularFireMessaging. EmanueleGuidotti. EmanueleGuidotti OPEN · Updated 9 months ago ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found