Skip to content

Commit

Permalink
Button to send a push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jellybob committed Apr 15, 2024
1 parent 531276e commit 10df98f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 16 additions & 2 deletions apps/notifications/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from wtforms import SubmitField, BooleanField
from apps.common import json_response
from main import db
from flask import render_template, request, current_app as app
from flask import render_template, request, current_app as app, flash, redirect, url_for
from flask_login import current_user, login_required

from . import notifications
from ..common.forms import Form
from models.web_push import public_key, WebPushTarget
from models.web_push import public_key, WebPushTarget, notify
from models.notifications import UserNotificationPreference


Expand Down Expand Up @@ -37,6 +37,20 @@ def index():
)


@notifications.route("/test", methods=["POST"])
@login_required
def test():
if len(current_user.web_push_targets) == 0:
flash("You have no devices configured for push notifications.")
return redirect(url_for("notifications.index"))

for target in current_user.web_push_targets:
notify(target, "This is a test notification.")

flash("Test notifications have been sent.")
return redirect(url_for("notifications.index"))


@notifications.route("/register", methods=["POST"])
@json_response
@login_required
Expand Down
3 changes: 3 additions & 0 deletions templates/notifications/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ <h3 class="panel-title">Push Notifications</h3>
<div class="state" id="notification-state-granted">
<p>You have enabled push notifications on this device.</p>
<p>To disable them look in your browser settings.</p>
<form method="post" action="{{ url_for('notifications.test') }}">
<button type="submit" class="btn btn-primary">Test Notifications</button>
</form>
</div>
<div class="state" id="notification-state-denied">
<p>You have denied access to send push notifications to this device.</p>
Expand Down

0 comments on commit 10df98f

Please sign in to comment.