Skip to content

Commit 1460bb1

Browse files
fix(notification): improve web push payload
1 parent f12c6e3 commit 1460bb1

1 file changed

Lines changed: 46 additions & 14 deletions

File tree

raven_cloud/api/notification.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,30 @@ def _send(messages: Messages, site_url: str):
138138
data = sanitize_fcm_data(merged_data) if merged_data else None
139139

140140
if notif:
141-
# Intentionally NO `webpush.notification` here; together with `notification=None` below this is what prevents the web duplicate notification issue on PWAs.
142-
# `fcm_options.link` is a fallback click target for non-Chrome browsers where the SW
143-
# defers to default FCM click handling; `Urgency=high` tells the push service to deliver immediately instead of batching (needed for chat-style real-time pushes).
141+
# WebpushNotification carries title/body on the wire so FCM sends the push to Safari
142+
# via APNs with `apns-push-type: alert` (required for Safari Web Push / iOS PWA to
143+
# display anything - background/silent pushes are dropped by Apple). Chrome/Firefox
144+
# also accept it and our SW still owns rendering via `onBackgroundMessage` (the FCM
145+
# JS SDK suppresses auto-display when a background handler is registered), so there
146+
# is no duplicate notification.
147+
webpush_fcm_options = None
144148
if click_action and click_action.startswith('https://'):
145-
webpush = messaging.WebpushConfig(
146-
fcm_options=messaging.WebpushFCMOptions(link=click_action),
147-
headers={'Urgency': 'high'},
148-
)
149+
# `fcm_options.link` is a fallback click target for non-Chrome browsers where the SW
150+
# defers to default FCM click handling.
151+
webpush_fcm_options = messaging.WebpushFCMOptions(link=click_action)
152+
153+
webpush = messaging.WebpushConfig(
154+
notification=messaging.WebpushNotification(
155+
title=title,
156+
body=body,
157+
icon=image or None,
158+
tag=tag or (message.get('data') or {}).get('channel_id'),
159+
),
160+
fcm_options=webpush_fcm_options,
161+
# `Urgency=high` tells the push service to deliver immediately instead of
162+
# batching (needed for chat-style real-time pushes).
163+
headers={'Urgency': 'high'},
164+
)
149165

150166
# temp? don't send image to android for now
151167
# - priority='high' (both levels): wakes device out of Doze for prompt delivery.
@@ -343,14 +359,30 @@ def _send_to_users(messages: Messages, site_url: str):
343359
data = sanitize_fcm_data(merged_data) if merged_data else None
344360

345361
if notif:
346-
# Intentionally NO `webpush.notification` here; together with `notification=None` below this is what prevents the web duplicate notification issue on PWAs.
347-
# `fcm_options.link` is a fallback click target for non-Chrome browsers where the SW
348-
# defers to default FCM click handling; `Urgency=high` tells the push service to deliver immediately instead of batching (needed for chat-style real-time pushes).
362+
# WebpushNotification carries title/body on the wire so FCM sends the push to Safari
363+
# via APNs with `apns-push-type: alert` (required for Safari Web Push / iOS PWA to
364+
# display anything - background/silent pushes are dropped by Apple). Chrome/Firefox
365+
# also accept it and our SW still owns rendering via `onBackgroundMessage` (the FCM
366+
# JS SDK suppresses auto-display when a background handler is registered), so there
367+
# is no duplicate notification.
368+
webpush_fcm_options = None
349369
if click_action and click_action.startswith("https://"):
350-
webpush = messaging.WebpushConfig(
351-
fcm_options=messaging.WebpushFCMOptions(link=click_action),
352-
headers={"Urgency": "high"},
353-
)
370+
# `fcm_options.link` is a fallback click target for non-Chrome browsers where the SW
371+
# defers to default FCM click handling.
372+
webpush_fcm_options = messaging.WebpushFCMOptions(link=click_action)
373+
374+
webpush = messaging.WebpushConfig(
375+
notification=messaging.WebpushNotification(
376+
title=title,
377+
body=body,
378+
icon=image or None,
379+
tag=tag or (message.get("data") or {}).get("channel_id"),
380+
),
381+
fcm_options=webpush_fcm_options,
382+
# `Urgency=high` tells the push service to deliver immediately instead of
383+
# batching (needed for chat-style real-time pushes).
384+
headers={"Urgency": "high"},
385+
)
354386

355387
# temp? don't send image to android for now
356388
# - priority='high' (both levels): wakes device out of Doze for prompt delivery.

0 commit comments

Comments
 (0)