Push Notification Dependency - using device token

Hello i i try to follow all steps on https://push-notifications.anvil.app/ and when i pass the

from Push_Notifications import firebase

device_token = firebase.request_push_notifications()
print(device_token)

i lunch it on new project and new form i add the dependency but the page stay complitly empty and on console is not printing the variable “device_token”
How can be fixed ?
The issue can be from some google auth prametrs to configure ?
If i can have more information can be great! Thank you!

Are you trying to add this to the form init method? Because that’s usually a bad idea since it will block the rest of code. Try adding it to form_show.

1 Like

Hello, thanks for replay, i try to put it on show like here:

Blockquote
from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
import anvil.facebook.auth
import anvil.google.auth, anvil.google.drive
from anvil.google.drive import app_files
import anvil.users
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from Push_Notifications import firebase

#device_token = firebase.request_push_notifications()
#print(device_token)
class Form1(Form1Template):
def init(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
#anvil.users.login_with_form()
# Any code you write here will run when the form opens.

def form_show(self, **event_args):
device_token = firebase.request_push_notifications()

c=app_tables.users.add_row(token=device_token)
print(c)

"""This method is called when the column panel is shown on the screen"""
pass

“”"

from the console is not print anytingh and not gives some error but always same think is not show the token.
Can be someting to be change on the dependency code i mean on the:
“”"
serviceworker=navigator.serviceWorker.register(“_/api/service-worker.js”)
“”"
or some bad configuration on the Api of firebase?
Any help can be helpfull! Thank you!

Blockquote

Here the clone of main app:
https://anvil.works/build#clone:S3PN7YHBV7KNJPSQ=SBMP3J2CNVFNI6AHBJOU4LOE
Here the dependency:
https://anvil.works/build#clone:GO7TKU4QB4FXPGDH=35RKHYAE2YD6CFKUYRW33WVO

I try to follow more times the istruction on: https://push-notifications.anvil.app/
but when i try to print the token is not print

It appears to be working fine on my end. Do you see a popup asking you to allow push notifications on your browser?

1 Like

i change from google chrome to Opera browser and yes is return the popup to activate notification, but on console is not print the token, or what can be the best approch to store it on the datatable?

I mean how i can see or store the token fro device here:


I need to activate Google Api on dependency or on the main and how to start the process to make it store correctly the token ?
Thank you!

Handling the device token is not a part of the dependency. You can just create a new column and write the code for storing the device token there.

After that, you use that device token later to send push notifications to the device.

1 Like

Ok but if device_token return current token ?
Because if i print it the console it will stay empty

i try to add with

Blockquote
app.table.users.add_row() if user make login true and this is ok i add this to datatable:


I run and is give me error on http like here ERROR 500:

After i try to change this path in your code:

And i try to play with this part too:

There are something to change on dependency or on api about the dependency ? What can be the issue?
Thank you!

I’ll recommend checking your app logs for errors that may have occurred in incoming http request.

I find the issue it was something on server side of main app, i clean all and now is fixed and the funtion device_token return correctly the string of token!Thank you @divyeshlakhotia
Now i will go for next step call the push_notifications.send(device_token,‘Title’,‘Body’,‘Logo’,‘Link’)
it can be possible to send something like this:
push_notifications.send(device_token,‘Title’,‘Body’,‘’,‘’) without link and logo ?
Thank you!

Yes, it should work. Also, I will soon be making some changes to the dependency which will enable some more functionalities.

1 Like

This is great i will monitoring your app for more futures, you are genius, thank you for help! :pray:

Hello, here again :sweat_smile:
I’m trying to call the function on my VPS :

from Push_Notifications import push_notifications
push_notifications.send(device_token,'Title','Body','Logo','Link')

when i run it is ask me the module push_notifications is not define, im trying to connect the dependency with the uplink and i do try with uplink of main app too.
There is some way to call correctly the module to make the func. push_notifications.send work and triggered when is find condition on my vps

Thank you!

Since you don’t have the push_notifications module in your local machine, it will not work. However, you can manually add the function to your machine from the push notifications dependency.

Since yesterday im trying to send the notifycation withou success till now in my console when have this string:
Capture72
i correctly pass on the function the device_token but it seem like the api or sometingh in json as not work fine what can generate this kind of error?

I do some other test and is return:
Capture73
but the notification always not show

I try some other tests and gives succes:1, no error on console but always not show notification, i try on google chrome, mozilla, opera and android, the token for each device is stored fine but when i run some logic to trigger the notification is not work here the clone of the app:
https://anvil.works/build#clone:S3PN7YHBV7KNJPSQ=SBMP3J2CNVFNI6AHBJOU4LOE
and here the clone of dependency:
https://anvil.works/build#clone:GO7TKU4QB4FXPGDH=35RKHYAE2YD6CFKUYRW33WVO

And this is dispayed on console:

Any help can be great!

If it shows success, the notification should have been delivered. The issue is with the device. Please try the following steps

  1. Change the site permissions for your Anvil App from the browser and set ‘Notifications’ to ‘Ask’
  2. Get a new device token and try sending a notification to that device token.

Hi,
The link to clone of the app does not seem to work. Is it possible for you to share it again?