MatthieuLemoine / electron-push-receiver

A module to bring Web Push support to Electron allowing it to receive notifications from Firebase Cloud Messaging (FCM).
https://medium.com/@MatthieuLemoine/my-journey-to-bring-web-push-support-to-node-and-electron-ce70eea1c0b0
MIT License
191 stars 62 forks source link

Exception from outdated electron-config module on newer version of Electron #86

Open RandomEngy opened 2 years ago

RandomEngy commented 2 years ago

I'm using Electron 16.0.4 and am initializing the electron-push-receiver from the isolated context preload script in the renderer process. I'm getting this error on importing the module:

TypeError: Cannot read properties of undefined (reading 'app')
    at new ElectronConfig (webpack-internal:///../node_modules/electron-config/index.js:8:47)
    at eval (webpack-internal:///../node_modules/electron-push-receiver/src/index.js:12:16)

It appears that v1.0 of electron-config is making a check: electron.app || electron.remote.app. This throws since both are null in newer versions of Electron. It appears the new package for storing data is electron-store. I think it might be fixed just by updating to use that instead.

I was able to work around this by using the literal strings when listening for the push events rather than trying to import the values in the preload script.

"PUSH_RECEIVER:::START_NOTIFICATION_SERVICE"
"PUSH_RECEIVER:::NOTIFICATION_SERVICE_ERROR"
"PUSH_RECEIVER:::NOTIFICATION_SERVICE_STARTED"
"PUSH_RECEIVER:::NOTIFICATION_SERVICE_ERROR"
"PUSH_RECEIVER:::NOTIFICATION_RECEIVED"
"PUSH_RECEIVER:::TOKEN_UPDATED"
davej commented 2 years ago

Can confirm. I noticed the same issue.

vialoh commented 2 years ago

@RandomEngy I just want to say... Thank you for sharing your workaround. You probably saved me hours/days of going in circles trying to find a solution. It would have taken me entirely too long to determine that importing the constants from electron-push-receiver was the reason for my preload script failing to load, based on the console's error messages. Seriously, thank you.

weeks6 commented 2 years ago

@vialoh Hi, can you please share your snippet of how you initialize the reciever? I tried using strings instead of imported event names but I still get the same error

vialoh commented 2 years ago

@weeks6 Sure! I'm using @capacitor-community/electron, and you can see how I've integrated electron-push-receiver here in this commit: https://github.com/molecule-dev/molecule-app/commit/762188931c98f2835718e147abd4a5c934af555c

And if you're curious as to how it's integrated into a React app: https://github.com/molecule-dev/molecule-app/commit/b4b94cd9c31e559dc250835dfb099b1ac008b59e

Plus some instructions for configuring the GOOGLE_CLOUD_MESSAGING_SENDER_ID: https://github.com/molecule-dev/molecule-app/commit/dd767c87625448c0ca5e19378b202379c429453a

The molecule-dev/molecule-app is a work in progress which often has its git history overwritten (for good reason if you take a look at what it's for), so if these links to the commits above ever break, ping me here and I'll update this comment.

weeks6 commented 2 years ago

@vialoh Thanks! That helped me a great deal!