# Buggy code
def handle_push_notification(user_profile_id, missed_message):
    # type: (int, Dict[str, Any]) -> None
    try:
        user_profile = get_user_profile_by_id(user_profile_id)
        if not (
            receives_offline_notifications(user_profile)
            or receives_online_notifications(user_profile)
        ):
            return

        umessage = UserMessage.objects.get(
            user_profile=user_profile, message__id=missed_message["message_id"]
        )
        message = umessage.message
        if umessage.flags.read:
            return

        apns_payload = get_apns_payload(message)
        gcm_payload = get_gcm_payload(user_profile, message)

        if uses_notification_bouncer():
            send_notifications_to_bouncer(user_profile_id, apns_payload, gcm_payload)
            return

        android_devices = list(
            PushDeviceToken.objects.filter(user=user_profile, kind=PushDeviceToken.GCM)
        )

        apple_devices = list(
            PushDeviceToken.objects.filter(user=user_profile, kind=PushDeviceToken.APNS)
        )

        # TODO: set badge count in a better way
        if apple_devices:
            send_apple_push_notification(
                user_profile.id, apple_devices, badge=1, zulip=apns_payload
            )

        if android_devices:
            send_android_push_notification(android_devices, gcm_payload)

    except UserMessage.DoesNotExist:
        logging.error(
            "Could not find UserMessage with message_id %s"
            % (missed_message["message_id"],)
        )


# Traceback (ConnectionError)
Traceback (most recent call last):
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/worker/queue_processors.py", line 108, in consume_wrapper
self.consume(data)
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/worker/queue_processors.py", line 246, in consume
handle_push_notification(data['user_profile_id'], data)
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/decorator.py", line 639, in wrapped_func
ret = func(*args, **kwargs)
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/lib/push_notifications.py", line 366, in handle_push_notification
gcm_payload)
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/lib/push_notifications.py", line 393, in send_notifications_to_bouncer
send_json_to_push_bouncer('POST', 'notify', post_data)
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/lib/push_notifications.py", line 456, in send_json_to_push_bouncer
extra_headers={"Content-type": "application/json"},
File "/home/zulip/deployments/2017-06-07-13-25-05/zerver/lib/push_notifications.py", line 476, in send_to_push_bouncer
headers=headers)
File "/home/zulip/deployments/2017-06-07-13-25-05/zulip-venv/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/home/zulip/deployments/2017-06-07-13-25-05/zulip-venv/lib/python2.7/site-packages/requests/sessions.py", line 518, in request
resp = self.send(prep, **send_kwargs)
File "/home/zulip/deployments/2017-06-07-13-25-05/zulip-venv/lib/python2.7/site-packages/requests/sessions.py", line 639, in send
r = adapter.send(request, **kwargs)
File "/home/zulip/deployments/2017-06-07-13-25-05/zulip-venv/lib/python2.7/site-packages/requests/adapters.py", line 502, in send
raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host='push.zulipchat.com', port=443): Max retries exceeded with url: /api/v1/remotes/push/notify (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f0d641b8f50>: Failed to establish a new connection: [Errno -2] Name or service not known',))

# Fixed code
def handle_push_notification(user_profile_id, missed_message):
    # type: (int, Dict[str, Any]) -> None
    """
    missed_message is the event received by the
    zerver.worker.queue_processors.PushNotificationWorker.consume function.
    """
    try:
        user_profile = get_user_profile_by_id(user_profile_id)
        if not (
            receives_offline_notifications(user_profile)
            or receives_online_notifications(user_profile)
        ):
            return

        umessage = UserMessage.objects.get(
            user_profile=user_profile, message__id=missed_message["message_id"]
        )
        message = umessage.message
        if umessage.flags.read:
            return

        apns_payload = get_apns_payload(message)
        gcm_payload = get_gcm_payload(user_profile, message)

        if uses_notification_bouncer():
            try:
                send_notifications_to_bouncer(
                    user_profile_id, apns_payload, gcm_payload
                )
            except requests.ConnectionError:
                if "failed_tries" not in missed_message:
                    missed_message["failed_tries"] = 0

                def failure_processor(event):
                    # type: (Dict[str, Any]) -> None
                    logging.warning(
                        "Maximum retries exceeded for trigger:%s event:push_notification"
                        % (event["user_profile_id"])
                    )

                retry_event(
                    "missedmessage_mobile_notifications",
                    missed_message,
                    failure_processor,
                )

            return

        android_devices = list(
            PushDeviceToken.objects.filter(user=user_profile, kind=PushDeviceToken.GCM)
        )

        apple_devices = list(
            PushDeviceToken.objects.filter(user=user_profile, kind=PushDeviceToken.APNS)
        )

        # TODO: set badge count in a better way
        if apple_devices:
            send_apple_push_notification(
                user_profile.id, apple_devices, badge=1, zulip=apns_payload
            )

        if android_devices:
            send_android_push_notification(android_devices, gcm_payload)

    except UserMessage.DoesNotExist:
        logging.error(
            "Could not find UserMessage with message_id %s"
            % (missed_message["message_id"],)
        )