Skip to content

Commit

Permalink
fix(notification): fix notification handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsCalebJones committed Nov 12, 2024
1 parent 701bc46 commit 49a936b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
23 changes: 16 additions & 7 deletions src/bot/app/events/notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,33 @@ def send_notification(self, event, event_type, webcast: bool = False):
data = self.build_data(event, event_type)

# Send Android notif
self.send_to_fcm(self.build_v3_topics(), data)
self.send_to_fcm(self.build_v3_topics(), data, webcast)

# Send Flutter notif
self.send_flutter_to_fcm(self.build_flutter_v3_topics(), data, webcast)

def send_to_fcm(self, topics, data):
def send_to_fcm(self, topics, data, webcast: bool = False):
logger.info("----------------------------------------------------------")
logger.info("Notification Data: %s" % data)
logger.info("Topics: %s" % topics)
notification = self.fcm.notify(data_payload=data, topic_condition=topics)
logger.info(f"Notification Data: {data}")
logger.info(f"Topics: {topics}")

event_info = json.loads(data["event"])
message_body = "Live webcast is available!" if webcast else event_info["description"]

notification = self.fcm.notify(
topic_condition=topics,
notification_title=event_info["name"],
notification_body=message_body,
)

logger.info(notification)
logger.info("----------------------------------------------------------")

def send_flutter_to_fcm(self, topics, data, webcast: bool = False):
logger.info("----------------------------------------------------------")
logger.info("Flutter Notification")
logger.info("Notification Data: %s" % data)
logger.info("Topics: %s" % topics)
logger.info(f"Notification Data: {data}")
logger.info(f"Topics: {topics}")

event_info = json.loads(data["event"])

Expand Down
18 changes: 10 additions & 8 deletions src/bot/app/notifications/news_notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def send_v3_notification(self, article, data):

def send_to_fcm(self, article, data, topics, flutter_topics):
logger.info("----------------------------------------------------------")
logger.info("Sending News notification - %s" % article.title)
logger.info(f"Sending News notification - {article.title}")
news_info = json.loads(data["item"])
try:
logger.info("News Notification Data - %s" % data)
logger.info("Topics - %s" % topics)
logger.info(f"News Notification Data - {data}")
logger.info(f"Topics - {topics}")
android_result = self.fcm.notify(
data_payload=data,
notification_title="New article via " + news_info["news_site_long"],
notification_body=news_info["title"],
topic_condition=topics,
)
logger.info(android_result)
Expand All @@ -51,11 +53,11 @@ def send_to_fcm(self, article, data, topics, flutter_topics):
logger.info("----------------------------------------------------------")

logger.info("----------------------------------------------------------")
logger.info("Sending News Flutter notification - %s" % article.title)
logger.info(f"Sending News Flutter notification - {article.title}")
try:
logger.info("News Notification Data - %s" % data)
logger.info("Topics - %s" % flutter_topics)
news_info = json.loads(data["item"])
logger.info(f"News Notification Data - {data}")
logger.info(f"Topics - {flutter_topics}")

flutter_results = self.fcm.notify(
data_payload=data,
topic_condition=flutter_topics,
Expand Down
42 changes: 40 additions & 2 deletions src/bot/app/notifications/notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ def send_v3_notification(self, launch: Launch, notification_type: str, contents:
"webcast": str(webcast),
}

all_result = self.send_notif_v3(
all_result = self.send_notif_v4(
data=data,
topics=get_fcm_all_topics_v3(debug=self.DEBUG, notification_type=notification_type),
analytics_label=f"notification_all_{data['launch_uuid']}",
)

strict_result = self.send_notif_v3(
strict_result = self.send_notif_v4(
data=data,
topics=get_fcm_strict_topics_v3(launch, debug=self.DEBUG, notification_type=notification_type),
analytics_label=f"notification_strict_{data['launch_uuid']}",
Expand Down Expand Up @@ -264,6 +264,44 @@ def send_notif_v3(
error=e,
)

def send_notif_v4(
self, data, topics, message_title=None, message_body=None, analytics_label: str = None
) -> NotificationResult:
try:
logger.info(f"Notification v4 Data - {data}")
logger.info(f"Topic Data v4- {topics}")

launch_image = None
if data.get("launch_image"):
launch_image = data.get("launch_image")

results = self.fcm.notify(
topic_condition=topics,
notification_title=message_title,
notification_body=message_body,
notification_image=launch_image,
fcm_options={"analytics_label": analytics_label},
android_config={"priority": "high", "collapse_key": data["launch_uuid"], "ttl": "86400s"},
timeout=240,
)
logger.info(results)
return NotificationResult(
notification_type=data["notification_type"],
topics=topics,
result=results,
analytics_label=analytics_label,
error=None,
)
except Exception as e:
logger.error(e)
return NotificationResult(
notification_type=data["notification_type"],
topics=topics,
result=results,
analytics_label=analytics_label,
error=e,
)

def send_custom_ios_v3(self, pending) -> NotificationResult:
data = self.get_json_data(pending)
label = "notification_custom_ios"
Expand Down
2 changes: 1 addition & 1 deletion src/bot/management/commands/run_send_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def handle(self, *args, **options):
.order_by("net", "id")
.distinct()
)
for launch in launches[:1]:
for launch in launches[:2]:
notification_obj, created = LaunchNotificationRecord.objects.get_or_create(launch_id=launch.id)
notification.send_notification(launch, "twentyFourHour", notification_obj)

Expand Down

0 comments on commit 49a936b

Please sign in to comment.