What is the simplest way to send notification from Elixir app to Android device?

Hey, I would like to create app for personally usage and connect it simply by Bluetooth or Wi-Fi IPv4 to my Android device. I would like to simply display notification that opens page in default browser on tap (you can think about it like about feed reader).

Originally I through about a feature like send files for Bluetooth - just directly connect two devices.

Another option that I have in mind is that I can create simply page in Phoenix and I will keep it always opened in browser.

Do you have any experience with similar topic? Can you recommend something for me? Is there an Elixir library for that?

Note: this app will be small - like “~5min. app” (only for me) with notifications, so I would like to keep it as simple as possible, so I prefer to don’t write any Android apps and use any 3rd party apps/servers.

look at “web push” - there is a lot of boilerplate and serviceworker setup and what not.

doesn’t look like there are any elixir libs at the momemt https://github.com/web-push-libs/ - which is actually pretty weird as web push is widely supported nowadays (iphone being the odd one out - but mac safari has support.)

here is supported browsers https://documentation.onesignal.com/v3.0/docs/web-push-setup

edit: there is https://github.com/tuvistavie/elixir-web-push-encryption - maybe all what is needed… dunno.

1 Like

I believe you can use FCM (aka GCM) for sending notifications to chrome browser. (Mobile or Desktop)

However you right now this service only supports calling a simple function in a javascript lib that you provide to browser. Which means you should fetch new data from server when this function is called. (I’m not 100% sure about this)

Here is a good FCM implementation in elixir: https://github.com/codedge-llc/pigeon

Here are docs about FCM browser notifications:
https://firebase.google.com/docs/cloud-messaging/js/client

P.S: i think payloads are supported in chrome>50 (not sure)

1 Like

@outlog: Please correct me if I’m wrong:

  1. Service Worker on my Android device could work even if I close browser (I read something about it), right? Or I need to “minimize” browser and then launch another app?
  2. If answer for 1st is true then as I understand it will only consume CPU and memory that is required only for that Worker, right?
  3. On server I need to send push message by FCM to Service Worker that will firstly display notification (at least in my case) and on click that notification it will open browser, right?

If answer for all that questions is true then it’s ideal solution for me!

go to https://webpush.rocks

click subscribe and then copy the curl cmd.

add a “url”: element to it

curl -XPOST -H "Content-type: application/json" -d '{
    "title": "This is a test",
    "description": "This is a longer description",
    "icon": "https://elixirforum.com/user_avatar/elixirforum.com/eiji/240/3774_1.png",
    "image": "https://elixirforum.com/user_avatar/elixirforum.com/eiji/240/3774_1.png",
    "url": "https://elixirforum.com"
}' 'https://webpush.rocks/send?id=https://fcm.googleapis.com/fcm/send/XXXXXXXXXXXXXXXX'

and of you go. (run the curl…)

tested it on android - killing chrome (all apps) and restarting phone - push works :slight_smile:

would love to see elixir code for this kind of thing, think it doesn’t take much code, but the docs are a bit of a rabbit hole of technical info - but surely some node/python etc implementation is there for inspiration.

1 Like

looked a bit further and elixir-web-push-encryption seems to do it already in elixir… just need a gcm key from google.

js: https://github.com/tuvistavie/elixir-web-push-encryption/tree/master/client-sample (maybe PR in support for url etc in that js code)

let me know how it goes.

1 Like

I have good experiences with prowl (only ios) and pushover. Youll have to install their app, but then it just works. I switched to pushover because then each user can manage their subscriptions themselves on the pushover web page. Pushover app is $5 i believe then free usage for low volume

2 Likes

ok, I checked all propositions and here is what I have:

  1. Generate keys from Push Companion
  2. Create Phoenix app and add these keys to project configuration
  3. Create a template that output JavaScript code with that key
  4. Get subscription details and send them on server
  5. Save that data (from example in database)
  6. Send message
  7. Create notification for that message

From what I can see the part that makes this work also after turn off device is Web Worker, so I do not need any 3rd-party providers (that could turn off service, request for any payments and increase/decrease limits - if any) and finally I can use it on all mobile devices. From here I’m thinking about:

  1. Create WebSocket Phoenix app
  2. Render template that register Web Worker
  3. On Web Worker start initialize WebSocket connection
  4. On WebSocket message display notification

This one looks much more simpler especially because WebSocket is really well supported in Phoenix and I have also experience with it. What do you think @outlog?

Battery life, and ‘it just works’ makes me recommend going with web push, honestly I would start out on the webpush.rocks service and then do a json post from my server - that is literally a 2 minute setup with no complexity, no impact on battery and it works in all scenarios.

then save the todo on setting it up on your own server with the service_worker.js, encryption keys, ssl requirements etc. for a rainy day.

I’m unsure of the websocket solution, and it will hurt battery life. but it might work…

1 Like

@outlog: I’m not sure about battery life. In both situations we have Web Worker that waits for message. In first case we are waiting for push (I don’t know how exactly it works) and in second situation we have WebSocket. WebSocket provides stable channel between client-server, so we do not need to send more that one request data (for example headers), so in some cases it’s really better than AJAX. From here if push is like AJAX then WebSocket is better. If if works similarly to WebSocket then I do not see where it should consume more battery.

Both situations are really easy to setup too. it just works yes, but now - server rules could change, for example free service could be changed to paid service, there could be limits for number of messages or message size or simply service could be not supported in feature, because in “Android Z” there will be “better” technology. With WebSocket I’m sure that my app will work always, so it’s why I decided to choose WebSocket way.

I do not need to setup any security, because my app will work only locally.

Thank you all for your help!
:slight_smile:

1 Like