push-notifications: Update payload for realm name and event string.

Adds `user.realm.string_id` as the realm name to the base payload
for notifications. Uses this realm name in the body of the alert
in the `apns_data`.

Changes the event string from "test-by-device-token" to "test".

Fixes #28075.
This commit is contained in:
Lauryn Menard 2023-12-07 19:03:21 +01:00 committed by Tim Abbott
parent 2165486c88
commit a018f2611b
5 changed files with 44 additions and 8 deletions

View File

@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 8.0 ## Changes in Zulip 8.0
**Feature level 234**
* Mobile push notifications now include a `realm_name` field.
* [`POST /mobile_push/test_notification`](/api/test-notify) now sends
a test notification with `test` rather than `test-by-device-token`
in the `event` field.
**Feature level 233** **Feature level 233**
* [`POST /register`](/api/register-queue), [`GET /events`](/api/get-events): * [`POST /register`](/api/register-queue), [`GET /events`](/api/get-events):

View File

@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# Changes should be accompanied by documentation explaining what the # Changes should be accompanied by documentation explaining what the
# new level means in api_docs/changelog.md, as well as "**Changes**" # new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`. # entries in the endpoint's documentation in `zulip.yaml`.
API_FEATURE_LEVEL = 233 API_FEATURE_LEVEL = 234
# Bump the minor PROVISION_VERSION to indicate that folks should provision # Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump # only when going from an old version of the code to a newer version. Bump

View File

@ -915,6 +915,7 @@ def get_base_payload(user_profile: UserProfile) -> Dict[str, Any]:
data["server"] = settings.EXTERNAL_HOST data["server"] = settings.EXTERNAL_HOST
data["realm_id"] = user_profile.realm.id data["realm_id"] = user_profile.realm.id
data["realm_uri"] = user_profile.realm.uri data["realm_uri"] = user_profile.realm.uri
data["realm_name"] = user_profile.realm.name
data["user_id"] = user_profile.id data["user_id"] = user_profile.id
return data return data
@ -1405,7 +1406,7 @@ def send_test_push_notification_directly_to_devices(
remote: Optional["RemoteZulipServer"] = None, remote: Optional["RemoteZulipServer"] = None,
) -> None: ) -> None:
payload = copy.deepcopy(base_payload) payload = copy.deepcopy(base_payload)
payload["event"] = "test-by-device-token" payload["event"] = "test"
apple_devices = [device for device in devices if device.kind == PushDeviceToken.APNS] apple_devices = [device for device in devices if device.kind == PushDeviceToken.APNS]
android_devices = [device for device in devices if device.kind == PushDeviceToken.GCM] android_devices = [device for device in devices if device.kind == PushDeviceToken.GCM]
@ -1415,10 +1416,13 @@ def send_test_push_notification_directly_to_devices(
android_payload = copy.deepcopy(payload) android_payload = copy.deepcopy(payload)
realm_uri = base_payload["realm_uri"] realm_uri = base_payload["realm_uri"]
realm_name = base_payload["realm_name"]
apns_data = { apns_data = {
"alert": { "alert": {
"title": _("Test notification"), "title": _("Test notification"),
"body": _("This is a test notification from {realm_uri}.").format(realm_uri=realm_uri), "body": _("This is a test notification from {realm_name} ({realm_uri}).").format(
realm_name=realm_name, realm_uri=realm_uri
),
}, },
"sound": "default", "sound": "default",
"custom": {"zulip": apple_payload}, "custom": {"zulip": apple_payload},

View File

@ -9503,7 +9503,13 @@ paths:
This endpoint allows a user to trigger a test push notification to their This endpoint allows a user to trigger a test push notification to their
selected mobile device, or all their mobile devices. selected mobile device, or all their mobile devices.
**Changes**: New in Zulip 8.0 (feature level 217). **Changes**: Starting with Zulip 8.0 (feature level 234), test
notifications sent via this endpoint use `test` rather than
`test-by-device-token` in the `event` field. (All mobile push
notifications also now include a `realm_name` field as well as
of this feature level).
New in Zulip 8.0 (feature level 217).
parameters: parameters:
- name: token - name: token
in: query in: query

View File

@ -197,9 +197,10 @@ class SendTestPushNotificationEndpointTest(BouncerTestCase):
expected_android_payload = { expected_android_payload = {
"server": "testserver", "server": "testserver",
"realm_id": user.realm_id, "realm_id": user.realm_id,
"realm_name": "Zulip Dev",
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": user.id, "user_id": user.id,
"event": "test-by-device-token", "event": "test",
"time": datetime_to_timestamp(time_now), "time": datetime_to_timestamp(time_now),
} }
expected_gcm_options = {"priority": "high"} expected_gcm_options = {"priority": "high"}
@ -220,16 +221,17 @@ class SendTestPushNotificationEndpointTest(BouncerTestCase):
expected_apple_payload = { expected_apple_payload = {
"alert": { "alert": {
"title": "Test notification", "title": "Test notification",
"body": "This is a test notification from http://zulip.testserver.", "body": "This is a test notification from Zulip Dev (http://zulip.testserver).",
}, },
"sound": "default", "sound": "default",
"custom": { "custom": {
"zulip": { "zulip": {
"server": "testserver", "server": "testserver",
"realm_id": user.realm_id, "realm_id": user.realm_id,
"realm_name": "Zulip Dev",
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": user.id, "user_id": user.id,
"event": "test-by-device-token", "event": "test",
} }
}, },
} }
@ -302,9 +304,10 @@ class SendTestPushNotificationEndpointTest(BouncerTestCase):
expected_payload = { expected_payload = {
"server": "testserver", "server": "testserver",
"realm_id": user.realm_id, "realm_id": user.realm_id,
"realm_name": "Zulip Dev",
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": user.id, "user_id": user.id,
"event": "test-by-device-token", "event": "test",
"time": datetime_to_timestamp(time_now), "time": datetime_to_timestamp(time_now),
} }
expected_gcm_options = {"priority": "high"} expected_gcm_options = {"priority": "high"}
@ -2390,6 +2393,7 @@ class HandlePushNotificationTest(PushNotificationTest):
"zulip": { "zulip": {
"server": "testserver", "server": "testserver",
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": self.user_profile.id, "user_id": self.user_profile.id,
"event": "remove", "event": "remove",
@ -2400,6 +2404,7 @@ class HandlePushNotificationTest(PushNotificationTest):
{ {
"server": "testserver", "server": "testserver",
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": self.user_profile.id, "user_id": self.user_profile.id,
"event": "remove", "event": "remove",
@ -2453,6 +2458,7 @@ class HandlePushNotificationTest(PushNotificationTest):
{ {
"server": "testserver", "server": "testserver",
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": self.user_profile.id, "user_id": self.user_profile.id,
"event": "remove", "event": "remove",
@ -2470,6 +2476,7 @@ class HandlePushNotificationTest(PushNotificationTest):
"zulip": { "zulip": {
"server": "testserver", "server": "testserver",
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": "http://zulip.testserver", "realm_uri": "http://zulip.testserver",
"user_id": self.user_profile.id, "user_id": self.user_profile.id,
"event": "remove", "event": "remove",
@ -2949,6 +2956,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"sender_id": self.sender.id, "sender_id": self.sender.id,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": user_profile.id, "user_id": user_profile.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -2992,6 +3000,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"sender_id": self.sender.id, "sender_id": self.sender.id,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": user_profile.id, "user_id": user_profile.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -3024,6 +3033,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"topic": message.topic_name(), "topic": message.topic_name(),
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": self.sender.id, "user_id": self.sender.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -3062,6 +3072,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"topic": message.topic_name(), "topic": message.topic_name(),
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": user_profile.id, "user_id": user_profile.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -3099,6 +3110,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"topic": message.topic_name(), "topic": message.topic_name(),
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": user_profile.id, "user_id": user_profile.id,
"mentioned_user_group_id": user_group.id, "mentioned_user_group_id": user_group.id,
@ -3137,6 +3149,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"topic": message.topic_name(), "topic": message.topic_name(),
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": user_profile.id, "user_id": user_profile.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -3198,6 +3211,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"sender_id": self.sender.id, "sender_id": self.sender.id,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": self.sender.realm.id, "realm_id": self.sender.realm.id,
"realm_name": self.sender.realm.name,
"realm_uri": self.sender.realm.uri, "realm_uri": self.sender.realm.uri,
"user_id": user_profile.id, "user_id": user_profile.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -3247,6 +3261,7 @@ class TestGetAPNsPayload(PushNotificationTest):
"topic": message.topic_name(), "topic": message.topic_name(),
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": hamlet.realm.id, "realm_id": hamlet.realm.id,
"realm_name": hamlet.realm.name,
"realm_uri": hamlet.realm.uri, "realm_uri": hamlet.realm.uri,
"user_id": polonius.id, "user_id": polonius.id,
"time": datetime_to_timestamp(message.date_sent), "time": datetime_to_timestamp(message.date_sent),
@ -3285,6 +3300,7 @@ class TestGetGCMPayload(PushNotificationTest):
"content_truncated": truncate_content, "content_truncated": truncate_content,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": hamlet.realm.id, "realm_id": hamlet.realm.id,
"realm_name": hamlet.realm.name,
"realm_uri": hamlet.realm.uri, "realm_uri": hamlet.realm.uri,
"sender_id": hamlet.id, "sender_id": hamlet.id,
"sender_email": hamlet.email, "sender_email": hamlet.email,
@ -3343,6 +3359,7 @@ class TestGetGCMPayload(PushNotificationTest):
"content_truncated": False, "content_truncated": False,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": hamlet.realm.id, "realm_id": hamlet.realm.id,
"realm_name": hamlet.realm.name,
"realm_uri": hamlet.realm.uri, "realm_uri": hamlet.realm.uri,
"sender_id": hamlet.id, "sender_id": hamlet.id,
"sender_email": hamlet.email, "sender_email": hamlet.email,
@ -3375,6 +3392,7 @@ class TestGetGCMPayload(PushNotificationTest):
"content_truncated": False, "content_truncated": False,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": hamlet.realm.id, "realm_id": hamlet.realm.id,
"realm_name": hamlet.realm.name,
"realm_uri": hamlet.realm.uri, "realm_uri": hamlet.realm.uri,
"sender_id": hamlet.id, "sender_id": hamlet.id,
"sender_email": hamlet.email, "sender_email": hamlet.email,
@ -3424,6 +3442,7 @@ class TestGetGCMPayload(PushNotificationTest):
"content_truncated": False, "content_truncated": False,
"server": settings.EXTERNAL_HOST, "server": settings.EXTERNAL_HOST,
"realm_id": hamlet.realm.id, "realm_id": hamlet.realm.id,
"realm_name": hamlet.realm.name,
"realm_uri": hamlet.realm.uri, "realm_uri": hamlet.realm.uri,
"sender_id": hamlet.id, "sender_id": hamlet.id,
"sender_email": f"user{hamlet.id}@zulip.testserver", "sender_email": f"user{hamlet.id}@zulip.testserver",