in firebase_admin/_messaging_encoder.py [0:0]
def encode_android_notification(cls, notification):
"""Encodes an ``AndroidNotification`` instance into JSON."""
if notification is None:
return None
if not isinstance(notification, _messaging_utils.AndroidNotification):
raise ValueError('AndroidConfig.notification must be an instance of '
'AndroidNotification class.')
result = {
'body': _Validators.check_string(
'AndroidNotification.body', notification.body),
'body_loc_args': _Validators.check_string_list(
'AndroidNotification.body_loc_args', notification.body_loc_args),
'body_loc_key': _Validators.check_string(
'AndroidNotification.body_loc_key', notification.body_loc_key),
'click_action': _Validators.check_string(
'AndroidNotification.click_action', notification.click_action),
'color': _Validators.check_string(
'AndroidNotification.color', notification.color, non_empty=True),
'icon': _Validators.check_string(
'AndroidNotification.icon', notification.icon),
'sound': _Validators.check_string(
'AndroidNotification.sound', notification.sound),
'tag': _Validators.check_string(
'AndroidNotification.tag', notification.tag),
'title': _Validators.check_string(
'AndroidNotification.title', notification.title),
'title_loc_args': _Validators.check_string_list(
'AndroidNotification.title_loc_args', notification.title_loc_args),
'title_loc_key': _Validators.check_string(
'AndroidNotification.title_loc_key', notification.title_loc_key),
'channel_id': _Validators.check_string(
'AndroidNotification.channel_id', notification.channel_id),
'image': _Validators.check_string(
'image', notification.image),
'ticker': _Validators.check_string(
'AndroidNotification.ticker', notification.ticker),
'sticky': notification.sticky,
'event_time': _Validators.check_datetime(
'AndroidNotification.event_timestamp', notification.event_timestamp),
'local_only': notification.local_only,
'notification_priority': _Validators.check_string(
'AndroidNotification.priority', notification.priority, non_empty=True),
'vibrate_timings': _Validators.check_number_list(
'AndroidNotification.vibrate_timings_millis', notification.vibrate_timings_millis),
'default_vibrate_timings': notification.default_vibrate_timings,
'default_sound': notification.default_sound,
'default_light_settings': notification.default_light_settings,
'light_settings': cls.encode_light_settings(notification.light_settings),
'visibility': _Validators.check_string(
'AndroidNotification.visibility', notification.visibility, non_empty=True),
'notification_count': _Validators.check_number(
'AndroidNotification.notification_count', notification.notification_count)
}
result = cls.remove_null_values(result)
color = result.get('color')
if color and not re.match(r'^#[0-9a-fA-F]{6}$', color):
raise ValueError(
'AndroidNotification.color must be in the form #RRGGBB.')
if result.get('body_loc_args') and not result.get('body_loc_key'):
raise ValueError(
'AndroidNotification.body_loc_key is required when specifying body_loc_args.')
if result.get('title_loc_args') and not result.get('title_loc_key'):
raise ValueError(
'AndroidNotification.title_loc_key is required when specifying title_loc_args.')
event_time = result.get('event_time')
if event_time:
# if the datetime instance is not naive (tzinfo is present), convert to UTC
# otherwise (tzinfo is None) assume the datetime instance is already in UTC
if event_time.tzinfo is not None:
event_time = event_time.astimezone(datetime.timezone.utc)
result['event_time'] = event_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
priority = result.get('notification_priority')
if priority:
if priority not in ('min', 'low', 'default', 'high', 'max'):
raise ValueError('AndroidNotification.priority must be "default", "min", "low", '
'"high" or "max".')
result['notification_priority'] = 'PRIORITY_' + priority.upper()
visibility = result.get('visibility')
if visibility:
if visibility not in ('private', 'public', 'secret'):
raise ValueError(
'AndroidNotification.visibility must be "private", "public" or "secret".')
result['visibility'] = visibility.upper()
vibrate_timings_millis = result.get('vibrate_timings')
if vibrate_timings_millis:
vibrate_timing_strings = []
for msec in vibrate_timings_millis:
formated_string = cls.encode_milliseconds(
'AndroidNotification.vibrate_timings_millis', msec)
vibrate_timing_strings.append(formated_string)
result['vibrate_timings'] = vibrate_timing_strings
return result