# Log action
log.info("%s subscribed to push notifications" % self)
- # Send a message
- await subscription.send(
- self._make_message(
- _("Hello, %s!") % self,
- _("You have successfully subscribed to push notifications."),
- ),
- )
-
return subscription
- async def send_push_message(self, title, body, **kwargs):
+ async def send_push_message(self, *args, **kwargs):
"""
Sends a message to all active subscriptions
"""
if not subscriptions:
return False
- # Format the message
- message = self._make_message(title, body, **kwargs)
-
# Send the message to all subscriptions
for subscription in subscriptions:
- await subscription.send(message)
+ await subscription.send(*args, **kwargs)
return True
- def _make_message(self, title, body, **kwargs):
- """
- Formats a push message with the given attributes and some useful defaults
- """
- # Format the message as a dictionary
- message = {
- "title" : title,
- "body" : body,
- } | kwargs
-
- return message
-
class UserPushSubscription(database.Base, database.BackendMixin):
__tablename__ = "user_push_subscriptions"
def vapid_public_key(self):
return self.vapid_private_key.public_key()
- async def send(self, message, ttl=0):
+ async def send(self, title, body, ttl=None):
"""
Sends a message to the user using the push service
"""
- # Convert strings into a message object
- if isinstance(message, str):
- message = {
- "message" : message,
- }
+ message = {
+ "title" : title,
+ "body" : body,
+ }
- # Convert dict() to JSON
- if isinstance(message, dict):
- message = json.dumps(message)
+ # Optionally add the TTL
+ if ttl:
+ message["ttl"] = ttl
- # Encode everything as bytes
- if not isinstance(message, bytes):
- message = message.encode()
+ # Convert dict() to JSON
+ message = json.dumps(message)
# Encrypt the message
message = self._encrypt(message)
"""
headers = {}
+ # Encode everything as bytes
+ if not isinstance(message, bytes):
+ message = message.encode()
+
# Generate some salt
salt = os.urandom(16)