tinode / webapp

Tinode web chat using React
Apache License 2.0
315 stars 197 forks source link

Browser notifications #12

Closed pwFoo closed 5 years ago

pwFoo commented 6 years ago

"Browser push notifications are not implemented."

Is it planned in the near future? Would be great for desktop browser and closed browser notifications with mobile / android.

or-else commented 6 years ago

I would say it's a couple of months away.

pwFoo commented 6 years ago

@or-else Thanks for your quick reply! Do you plan to implement a fix solution like FCM or by browser / device decision (Chrome, Firefox, Edge)?

Push notifications would be awesome :)

or-else commented 6 years ago

FCM is already implemented in the server for mobile pushes, so it's going to be FCM-based for browsers too. https://github.com/tinode/chat/blob/master/server/push/fcm/push_fcm.go

BTW, you are welcome to help out with a pull request :)

pwFoo commented 6 years ago

I'm new with git pull requests and programming (golang, web notifications api, ...). So I don't know if I really can help here, but I'll take a look.

Tried a little bit with php / javascript based on that example which uses browser / vendor dependent push service.

e.g. Firebase Cloud Messaging (FCM), Mozilla Cloud Services (MCS), and Windows Push Notification Service (WNS) depending on which browser is being used

I would prefer to let the device choose the push service itself (Edge -> WNS, Chrome / Android -> FCM, FIrefox -> MCS).

or-else commented 6 years ago

FCM supports all browsers with support for Web Notifications API https://firebase.googleblog.com/2018/05/web-notifications-api-support-now.html

Which means everything but Safari is supported: https://caniuse.com/#feat=push-api

pwFoo commented 6 years ago

Ok, but FCM is related to google, right? So maybe some users use Edge / Firefox / Opera instead of Chrome to avoid Google? Chrome and Android is related to Google / Gmail and FCM would be fine. Client side could be done with Javascript (un-/subscribe) and send subscription to the Server.
Server use the client subscription with preferred service. How complexe is server side? Pub / priv key and call the subacriber service for example with curl? Service url is given by client service subscription.

or-else commented 6 years ago

It's a matter of practicality. FCM is done. Non-FCM pushes are not done. I personally have no time for it. If someone wants to implement them - great, I'll support the effort.

or-else commented 6 years ago

Any update on this?

pwFoo commented 6 years ago

I built cross browser notifications based on the demo. Reduzed to the needed code. But it needs some rewrite / code clean up first.

And I don't know how to implement to the react client. Any plans to separate html / templates and js to different files? Should be easier to read, modify or maintain.

or-else commented 6 years ago

I built cross browser notifications based on the demo

Great.

Any plans to separate html / templates and js to different files?

React components are expected to include html (jsx) and js in the same file.

pwFoo commented 6 years ago

Maybe I take a look into the tinode.js lib how to build a custom client, but at the moment I have no time to play with or rewrite the notification code. I think I need some weeks to do other tasks first...

After some rewrite I could send you the client side notification code to take a look?

or-else commented 6 years ago

After some rewrite I could send you the client side notification code to take a look?

Absolutely. Would be glad to see it.

I'll close this issue. Once you get to working on it, please open a new one or write to https://groups.google.com/d/forum/tinode

pwFoo commented 6 years ago

@or-else https://groups.google.com/forum/#!topic/tinode/k8LzEApbwTM With link to a demo / repo

pwFoo commented 5 years ago

@or-else Do I need a FCM account for push notifications?

or-else commented 5 years ago

Yes, you need an FCM account.

pwFoo commented 5 years ago

With webpush-go library there is no account needed, but at the moment I haven't time to rewrite / cleanup the client side javascript code...

Do you know an easy howto? I need to create a FCM account to add push to my tinode test system.

or-else commented 5 years ago

Go to https://console.firebase.google.com/ Create an account, create your project, configure it, start pushing. The FCM push is free.

pwFoo commented 5 years ago

Account and project created. projekt id added to docker run, but FCM_CRED_FILE?

or-else commented 5 years ago

Go to Service Accounts. It's there

or-else commented 5 years ago

https://console.firebase.google.com/project/YOUR-PROJECT-NAME-HERE/settings/serviceaccounts/adminsdk

or-else commented 5 years ago

And you need to enable messaging api at console.cloud.google.com

pwFoo commented 5 years ago

Ok. Activated messaging and found a snippet (node.js, java, python, go) on "Firebase Admin SDK" page, but no json file / path / url / content.

It looks like I have to add a file to the container web path?

or-else commented 5 years ago
screen shot 2018-09-22 at 16 37 31

Keep in mind that it exists, it works. Yes, it is complex. Yes, it is convoluted. But with enough persistence it works.

or-else commented 5 years ago

It looks like I have to add a file to the container web path?

Not sure what you mean by web path, just follow instructions here: https://github.com/tinode/chat/tree/devel/docker#enable-push-notifications

pwFoo commented 5 years ago

Strange...

Verified file and content is correct mapped inside container. And also checked generated tinode config... cred file isn't read as should?

tinode container runs as root, file in volume exists content is the downloaded json file I can output the json file configured in the tinode config as long as the container runs (docker exec before container stops)

But tinode container stops with (got the logs with a /var/log/ volume to have persistent logs without running container...)

2018/09/22 15:01:23 Server v0.15:/opt/tinode/tinode:v0.15.6-rc5; db: 'mysql'; pid 46; 2 process(es)
2018/09/22 15:01:23 Using config from '/opt/tinode/working.config'
2018/09/22 15:01:23 Running as a standalone server.
2018/09/22 15:01:23 Failed to initialize push notifications:missing credentials

browser dependent push service just work with a priv / pub key generated server side. No account needed. So if tinode switches in the future it would be easier to use push notifications.

pwFoo commented 5 years ago

Each time I get

2018/09/22 15:15:23 Failed to initialize push notifications:missing credentials

But don't know how to debug it.

pwFoo commented 5 years ago

It's a locale problem because there is no traffic to FCM outgoing. So why the creds not found if path / file in config is fine and the cred file exists?

or-else commented 5 years ago

It's my bug. The docker template has credential_file while the code expects credentials_file.

pwFoo commented 5 years ago

ok, could I fix it somewhere or by an additional env variable? Or should I wait for the next docker dev image?

or-else commented 5 years ago

I'll release a new docker with the fix tomorrow.

or-else commented 5 years ago

0.15.6 released.

pwFoo commented 5 years ago

Looks like there is still a problem? I'm able to start the tinode app after I changed to the latest image. Tested with Chrome, Chrome Incognito and a mobile. No push notifications.

2018/09/23 15:57:30 fcm push failed http error status: 403; reason: sender id does not match regisration token; code: mismatched-credential; details: The caller does not have permission
or-else commented 5 years ago

Yep. I forgot about hardcoded key and sender ID in the javascript files too: https://github.com/tinode/webapp/blob/master/src/webapp.jsx#L38 https://github.com/tinode/webapp/blob/master/service-worker.js#L7

Now I need to figure out how to pass them to the webapp in the container.

or-else commented 5 years ago

But the error you are getting means that the server is set up correctly.

pwFoo commented 5 years ago

Yes, it looks good, just the right credentials are missing.

or-else commented 5 years ago

0.15.7-rc1 takes two new credentials for the client-side.

pwFoo commented 5 years ago

My I have still problems after update.

image

I can't de-/activate the Notification alert checkbox by click / keyboard.

image

Same with mobile (can't check the checkbox). Desktop and mobile same problem after clear cache / temp data / cookie / ... and incognito browser too.

Developer tools console:

Failed to initialize push notifications {code: "messaging/bad-sender-id", message: "Messaging: Please ensure that 'messagingSenderId' …ebase.initializeApp(). (messaging/bad-sender-id).", stack: "FirebaseError: Messaging: Please ensure that 'mess…s://<DOMAIN>/umd/index.prod.js:1:70982)"}
or-else commented 5 years ago

Are you using correct sender ID?

If you connect to a running container

docker exec -it name-of-the-running-container /bin/bash

what's the content of /firebase-init.js?

cat /firebase-init.js
or-else commented 5 years ago

The content of /firebase-init.js is public, you can freely post it here.

or-else commented 5 years ago

Or, actually, see the contents of firebase-init.js in the browser: http://localhost:6060/firebase-init.js

pwFoo commented 5 years ago

File doesn't exist

/opt/tinode # cat /firebase-init.js
cat: can't open '/firebase-init.js': No such file or directory
pwFoo commented 5 years ago

Web file exists, but empty...

const FIREBASE_INIT={messagingSenderId: "", messagingVapidKey: ""};

I think something is wrong with my docker command... Let me check...

or-else commented 5 years ago

did you provide the values on the command line when launching the container? --env FCM_SENDER_ID=... --env FCM_VAPID_KEY=...

or-else commented 5 years ago

it's actually cat /static/firebase-init.js

pwFoo commented 5 years ago

Problem are the missing env variables... But where I get sender_id (fcm project_id?) and vapid key?

pwFoo commented 5 years ago

Looks like I fixed it. Found the needed values in firebase console and try to test it

pwFoo commented 5 years ago

FCM is... annoying

2018/09/25 18:30:23 fcm push failed http error status: 403; reason: sender id does not match regisration token; code: mismatched-credential; details: The caller does not have permission
or-else commented 5 years ago

This error means your client-side credentials do not match your server-side credentials.

or-else commented 5 years ago

You can try sending a push from the firebase console to the client side token. This way you can be sure that the client side is set up correctly.

pwFoo commented 5 years ago

Where can I send a test push?

It would be so easy with browser dependent push instead of FCM. I hope it'll switch in the (near) future. I think I'll test it tomorrow if I have some time... Frustrating to set it up :(