Skip to content

Commit 3d53c8f

Browse files
committed
Android: use high priority for notification-inducing message types
Android normal notifications can induce a long delay before the device gets woken up, and so we really want to use high notifications when we can. This gets complicated, though, because Google has some required threshold of high priority notifications to actual device notifications, but we cannot know in advance whether a message might induce a notification (i.e. new DM) or not (typing indicator, or message in a conversation with notifications disabled). And so, for now, we disable high priority for messages that we know cannot illicit a notification, and try high priority for those that might, and just hope Android's limits aren't too severe.
1 parent 856f53c commit 3d53c8f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

spns/notifiers/firebase.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,23 @@ def push_notification(msg: Message):
6969

7070
enc_payload = encrypt_notify_payload(data, max_msg_size=MAX_MSG_SIZE)
7171

72+
# Send messages that are likely to illicit a device notification as high priority: specifically,
73+
# has a body, and is in namespace 0 (DMs) or 11 (group "v2" messages).
74+
# This doesn't catch them all, e.g. typing notifications, but definitely excludes things like
75+
# data-too-big messages and config updates which definitely won't notify.
76+
priority = "high" if b"~" in data and data[b"n"] in (0, 11) else "normal"
77+
7278
device_token = data[b"&"].decode() # unique service id, as we returned from validate
7379

7480
msg = {
75-
'fcm_token': device_token,
76-
'data_payload': {
81+
"fcm_token": device_token,
82+
"data_payload": {
7783
"enc_payload": oxenc.to_base64(enc_payload),
7884
"spns": f"{SPNS_FIREBASE_VERSION}"
79-
}
85+
},
86+
"android_config": {
87+
"priority": priority,
88+
},
8089
}
8190

8291
global notify_queue, queue_lock

0 commit comments

Comments
 (0)