mirror of
https://github.com/zulip/zulip.git
synced 2025-11-19 05:58:25 +00:00
push notifications: Reword APNs payload alert titles.
Also, rename get_alert_from_message to get_gcm_alert. With the implementation of the and get_apns_alert_title and get_apns_alert_subtitle, the logic within get_alert_from_message is only relevant to the GCM payload, so we adjust the name accordingly. Progresses #9949. Resolves https://github.com/zulip/zulip-mobile/issues/1316.
This commit is contained in:
@@ -10,7 +10,7 @@ import re
|
|||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from typing import Any, Dict, List, Optional, SupportsInt, Tuple, Type, Union
|
from typing import Any, Dict, List, Optional, SupportsInt, Tuple, Type, Union, cast
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import IntegrityError, transaction
|
from django.db import IntegrityError, transaction
|
||||||
@@ -429,7 +429,7 @@ def push_notifications_enabled() -> bool:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_alert_from_message(message: Message) -> str:
|
def get_gcm_alert(message: Message) -> str:
|
||||||
"""
|
"""
|
||||||
Determine what alert string to display based on the missed messages.
|
Determine what alert string to display based on the missed messages.
|
||||||
"""
|
"""
|
||||||
@@ -519,6 +519,29 @@ def get_common_payload(message: Message) -> Dict[str, Any]:
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def get_apns_alert_title(message: Message) -> str:
|
||||||
|
"""
|
||||||
|
On an iOS notification, this is the first bolded line.
|
||||||
|
"""
|
||||||
|
if message.recipient.type == Recipient.HUDDLE:
|
||||||
|
recipients = cast(List[Dict[str, Any]], get_display_recipient(message.recipient))
|
||||||
|
return ', '.join(sorted(r['full_name'] for r in recipients))
|
||||||
|
elif message.is_stream_message():
|
||||||
|
return "#%s > %s" % (message.stream_name, message.topic_name(),)
|
||||||
|
# For personal PMs, we just show the sender name.
|
||||||
|
return message.sender.full_name
|
||||||
|
|
||||||
|
def get_apns_alert_subtitle(message: Message) -> str:
|
||||||
|
"""
|
||||||
|
On an iOS notification, this is the second bolded line.
|
||||||
|
"""
|
||||||
|
if message.trigger == "mentioned":
|
||||||
|
return message.sender.full_name + " mentioned you:"
|
||||||
|
elif message.recipient.type == Recipient.PERSONAL:
|
||||||
|
return ""
|
||||||
|
# For group PMs, or regular messages to a stream, just use a colon to indicate this is the sender.
|
||||||
|
return message.sender.full_name + ":"
|
||||||
|
|
||||||
def get_apns_payload(user_profile: UserProfile, message: Message) -> Dict[str, Any]:
|
def get_apns_payload(user_profile: UserProfile, message: Message) -> Dict[str, Any]:
|
||||||
zulip_data = get_common_payload(message)
|
zulip_data = get_common_payload(message)
|
||||||
zulip_data.update({
|
zulip_data.update({
|
||||||
@@ -528,7 +551,8 @@ def get_apns_payload(user_profile: UserProfile, message: Message) -> Dict[str, A
|
|||||||
content, _ = truncate_content(get_mobile_push_content(message.rendered_content))
|
content, _ = truncate_content(get_mobile_push_content(message.rendered_content))
|
||||||
apns_data = {
|
apns_data = {
|
||||||
'alert': {
|
'alert': {
|
||||||
'title': get_alert_from_message(message),
|
'title': get_apns_alert_title(message),
|
||||||
|
'subtitle': get_apns_alert_subtitle(message),
|
||||||
'body': content,
|
'body': content,
|
||||||
},
|
},
|
||||||
'badge': 0, # TODO: set badge count in a better way
|
'badge': 0, # TODO: set badge count in a better way
|
||||||
@@ -542,7 +566,7 @@ def get_gcm_payload(user_profile: UserProfile, message: Message) -> Dict[str, An
|
|||||||
data.update({
|
data.update({
|
||||||
'user': user_profile.email,
|
'user': user_profile.email,
|
||||||
'event': 'message',
|
'event': 'message',
|
||||||
'alert': get_alert_from_message(message),
|
'alert': get_gcm_alert(message),
|
||||||
'zulip_message_id': message.id, # message_id is reserved for CCS
|
'zulip_message_id': message.id, # message_id is reserved for CCS
|
||||||
'time': datetime_to_timestamp(message.pub_date),
|
'time': datetime_to_timestamp(message.pub_date),
|
||||||
'content': content,
|
'content': content,
|
||||||
|
|||||||
@@ -733,7 +733,38 @@ class TestAPNs(PushNotificationTest):
|
|||||||
payload)
|
payload)
|
||||||
|
|
||||||
class TestGetAPNsPayload(PushNotificationTest):
|
class TestGetAPNsPayload(PushNotificationTest):
|
||||||
def test_get_apns_payload(self) -> None:
|
def test_get_apns_payload_personal_message(self) -> None:
|
||||||
|
user_profile = self.example_user("othello")
|
||||||
|
message_id = self.send_personal_message(
|
||||||
|
self.example_email('hamlet'),
|
||||||
|
self.example_email('othello'),
|
||||||
|
'Content of personal message',
|
||||||
|
)
|
||||||
|
message = Message.objects.get(id=message_id)
|
||||||
|
message.trigger = 'private_message'
|
||||||
|
payload = apn.get_apns_payload(user_profile, message)
|
||||||
|
expected = {
|
||||||
|
'alert': {
|
||||||
|
'title': 'King Hamlet',
|
||||||
|
'subtitle': '',
|
||||||
|
'body': message.content,
|
||||||
|
},
|
||||||
|
'badge': 0,
|
||||||
|
'custom': {
|
||||||
|
'zulip': {
|
||||||
|
'message_ids': [message.id],
|
||||||
|
'recipient_type': 'private',
|
||||||
|
'sender_email': 'hamlet@zulip.com',
|
||||||
|
'sender_id': 4,
|
||||||
|
'server': settings.EXTERNAL_HOST,
|
||||||
|
'realm_id': message.sender.realm.id,
|
||||||
|
'realm_uri': message.sender.realm.uri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.assertDictEqual(payload, expected)
|
||||||
|
|
||||||
|
def test_get_apns_payload_huddle_message(self) -> None:
|
||||||
user_profile = self.example_user("othello")
|
user_profile = self.example_user("othello")
|
||||||
message_id = self.send_huddle_message(
|
message_id = self.send_huddle_message(
|
||||||
self.sender.email,
|
self.sender.email,
|
||||||
@@ -743,7 +774,8 @@ class TestGetAPNsPayload(PushNotificationTest):
|
|||||||
payload = apn.get_apns_payload(user_profile, message)
|
payload = apn.get_apns_payload(user_profile, message)
|
||||||
expected = {
|
expected = {
|
||||||
'alert': {
|
'alert': {
|
||||||
'title': "New private group message from King Hamlet",
|
'title': 'Cordelia Lear, King Hamlet, Othello, the Moor of Venice',
|
||||||
|
'subtitle': 'King Hamlet:',
|
||||||
'body': message.content,
|
'body': message.content,
|
||||||
},
|
},
|
||||||
'badge': 0,
|
'badge': 0,
|
||||||
@@ -765,7 +797,38 @@ class TestGetAPNsPayload(PushNotificationTest):
|
|||||||
}
|
}
|
||||||
self.assertDictEqual(payload, expected)
|
self.assertDictEqual(payload, expected)
|
||||||
|
|
||||||
def test_get_apns_payload_stream(self):
|
def test_get_apns_payload_stream_message(self):
|
||||||
|
# type: () -> None
|
||||||
|
user_profile = self.example_user("hamlet")
|
||||||
|
stream = Stream.objects.filter(name='Verona').get()
|
||||||
|
message = self.get_message(Recipient.STREAM, stream.id)
|
||||||
|
message.trigger = 'push_stream_notify'
|
||||||
|
message.stream_name = 'Verona'
|
||||||
|
payload = apn.get_apns_payload(user_profile, message)
|
||||||
|
expected = {
|
||||||
|
'alert': {
|
||||||
|
'title': '#Verona > Test Message',
|
||||||
|
'subtitle': 'King Hamlet:',
|
||||||
|
'body': message.content,
|
||||||
|
},
|
||||||
|
'badge': 0,
|
||||||
|
'custom': {
|
||||||
|
'zulip': {
|
||||||
|
'message_ids': [message.id],
|
||||||
|
'recipient_type': 'stream',
|
||||||
|
'sender_email': 'hamlet@zulip.com',
|
||||||
|
'sender_id': 4,
|
||||||
|
"stream": apn.get_display_recipient(message.recipient),
|
||||||
|
"topic": message.subject,
|
||||||
|
'server': settings.EXTERNAL_HOST,
|
||||||
|
'realm_id': message.sender.realm.id,
|
||||||
|
'realm_uri': message.sender.realm.uri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.assertDictEqual(payload, expected)
|
||||||
|
|
||||||
|
def test_get_apns_payload_stream_mention(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
user_profile = self.example_user("othello")
|
user_profile = self.example_user("othello")
|
||||||
stream = Stream.objects.filter(name='Verona').get()
|
stream = Stream.objects.filter(name='Verona').get()
|
||||||
@@ -775,7 +838,8 @@ class TestGetAPNsPayload(PushNotificationTest):
|
|||||||
payload = apn.get_apns_payload(user_profile, message)
|
payload = apn.get_apns_payload(user_profile, message)
|
||||||
expected = {
|
expected = {
|
||||||
'alert': {
|
'alert': {
|
||||||
'title': "New mention from King Hamlet",
|
'title': '#Verona > Test Message',
|
||||||
|
'subtitle': 'King Hamlet mentioned you:',
|
||||||
'body': message.content,
|
'body': message.content,
|
||||||
},
|
},
|
||||||
'badge': 0,
|
'badge': 0,
|
||||||
@@ -806,7 +870,8 @@ class TestGetAPNsPayload(PushNotificationTest):
|
|||||||
payload = apn.get_apns_payload(user_profile, message)
|
payload = apn.get_apns_payload(user_profile, message)
|
||||||
expected = {
|
expected = {
|
||||||
'alert': {
|
'alert': {
|
||||||
'title': "New private group message from King Hamlet",
|
'title': 'Cordelia Lear, King Hamlet, Othello, the Moor of Venice',
|
||||||
|
'subtitle': "King Hamlet:",
|
||||||
'body': "***REDACTED***",
|
'body': "***REDACTED***",
|
||||||
},
|
},
|
||||||
'badge': 0,
|
'badge': 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user