Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ html_coverage_report/
# credentials
credentials.json

# binary file where snapshots saved
dump.rdb

#logs
service.err.log
service.out.log
Expand Down
7 changes: 5 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from flask import Flask, render_template, request, Response
from flask import Flask, render_template, request, Response, jsonify
from flask_json import FlaskJSON
from flask_cors import CORS
from config import config
from service.push_notification import PushNotification
import os
import json


vapid_public_key = os.getenv("VAPID_PUBLIC_KEY")


Expand All @@ -23,7 +24,9 @@ def index():
@app.route("/notifications", methods=['POST', 'GET'])
def calendar_notifications():
PushNotification().send_notifications_to_subscribers()
return PushNotification.send_notifications(PushNotification)
result = PushNotification.send_notifications(PushNotification)
return result


@app.route("/channels", methods=['POST', 'GET'])
def create_channels():
Expand Down
67 changes: 67 additions & 0 deletions service/external_calls_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import requests
import os
import json

from flask import Flask
from pyfcm import FCMNotification

from config import config
from utilities.utility import save_to_db
import celery

config_name = os.getenv('APP_SETTINGS')
push_service = FCMNotification(api_key=os.getenv('FCM_API_KEY'))
api_token = os.getenv('USER_TOKEN')

notification_url = config.get(config_name).NOTIFICATION_URL
url = config.get(config_name).CONVERGE_MRM_URL
app = Flask(__name__)


class ExternalCallsNotifications():
#notify a single device and save notifications
@celery.task(name='push_notification-single-device')
def notify_device(firebase_token, calendar_id):
try:
results = push_service.notify_single_device(
registration_id = firebase_token,
message_body="success")
result = {}
result['results'] = results['results']
result['subscriber_info'] = firebase_token
result['platform'] = 'android'
result['calendar_id'] = calendar_id
save_to_db(result)
return results
except Exception as error:
results = error
result = {}
result['results'] = results
result['subscriber_info'] = firebase_token
result['platform'] = 'android'
result['calendar_id'] = calendar_id
save_to_db(result)
return error


#update the backend API method
@celery.task(name='push_notification-update-backend')
def update_backend(calendar_id):
try:
notify_api_mutation = (
{
'query':
"""
mutation {
mrmNotification ( calendarId: \"%s\" ) {
message
}
}
""" % calendar_id
}
)
headers = {'Authorization': 'Bearer %s' % api_token}
requests.post(url=url, json=notify_api_mutation, headers=headers)
except Exception as error:
return error

42 changes: 13 additions & 29 deletions service/push_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import ast

from helpers.database import db
from flask import jsonify, request, render_template, Flask
from flask import jsonify, request, render_template, Flask, Response
from pyfcm import FCMNotification
from pywebpush import webpush, WebPushException

from .external_calls_notification import ExternalCallsNotifications
from config import config
from helpers.credentials import Credentials
from utilities.utility import stop_channel, save_to_db
from apiclient import errors
from helpers.calendar import update_calendar
import celery


config_name = os.getenv('APP_SETTINGS')
push_service = FCMNotification(api_key=os.getenv('FCM_API_KEY'))
api_token = os.getenv('USER_TOKEN')
Expand Down Expand Up @@ -132,9 +132,10 @@ def manual_create_channels(self):
service = Credentials.set_api_credentials(self)
calendar = {}
calendars = []
channels = []
channels = [1]
for key in db.keys('*Calendar*'):
calendar = db.hgetall(key)
print(key)
calendar['key'] = key
calendars.append(calendar)

Expand Down Expand Up @@ -171,39 +172,22 @@ def send_notifications(self):
selected_calendar = calendar
break
if 'firebase_token' in selected_calendar.keys():
results = push_service.notify_single_device(
registration_id=selected_calendar['firebase_token'],
message_body="success")
result = {}
result['results'] = results['results']
result['subscriber_info'] = selected_calendar['firebase_token']
result['platform'] = 'android'
result['calendar_id'] = selected_calendar['calendar_id']
save_to_db(result)

firebase_token = selected_calendar['firebase_token']
calendar_id = selected_calendar['calendar_id']
ExternalCallsNotifications.notify_device.delay(firebase_token, calendar_id)

# Send an update to the backend API
if 'calendar_id' in selected_calendar.keys():
notify_api_mutation = (
{
'query':
"""
mutation {
mrmNotification ( calendarId: \"%s\" ) {
message
}
}
""" % selected_calendar['calendar_id']
}
)
headers = {'Authorization': 'Bearer %s' % api_token}
requests.post(url=url, json=notify_api_mutation, headers=headers)
calendar_id = selected_calendar['calendar_id']
ExternalCallsNotifications.update_backend.delay(calendar_id)
results = {
"message": "Notification has been sent"
}
return jsonify(results)

data = {
"message": "Notification received but no registered device"
}
response = jsonify(data)

return response

def send_web_notification(self, subscriber, calendar_id):
Expand Down
32 changes: 16 additions & 16 deletions templates/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
<h2>Push Notification Logs</h2>

<table>
<tr>
<th>Time</th>
<th>Results</th>
<th>Subscriber Info</th>
<th>Platform</th>
<th>Calendar_id</th>



</tr>
{% for item in result%}
<tr>
{% for value in item.values() %}
<td>{{value}}</td>
{% endfor %}
</tr>
{% endfor %}
<th>Time</th>
<th>Results</th>
<th>Subscriber Info</th>
<th>Platform</th>
<th>Calendar_id</th>



</tr>
{% for item in result%}
<tr>
{% for value in item.values() %}
<td>{{value}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

</body>
Expand Down