#NEWLINE# def send( self , notification ) : #NEWLINE# #INDENT# expiry_seconds = ( ( notification [ 'time_to_live_ts_bigint' ] - int ( round ( ( time . time ( ) * 1000 ) ) ) ) / 1000 ) #NEWLINE# if ( expiry_seconds < 0 ) : #NEWLINE# #INDENT# self . log . warning ( 'GCM: expired notification with sending_id: {0}; expiry_seconds: {1}' . format ( notification [ 'sending_id' ] , expiry_seconds ) ) #NEWLINE# notification [ 'status' ] = const . NOTIFICATION_EXPIRED #NEWLINE# return #NEWLINE# #UNINDENT# utm_source = 'pushnotification' #NEWLINE# utm_campaign = str ( notification [ 'campaign_id' ] ) #NEWLINE# utm_medium = str ( notification [ 'message_id' ] ) #NEWLINE# data = { #NEWLINE# #INDENT# 'title' : notification [ 'title' ] , #NEWLINE# 'message' : notification [ 'content' ] , #NEWLINE# 'url' : ( ( ( ( ( ( ( ( self . base_deeplink_url + '://' ) + notification [ 'screen' ] ) + '?utm_source=' ) + utm_source ) + '&utm_campaign=' ) + utm_campaign ) + '&utm_medium=' ) + utm_medium ) , #NEWLINE# 'notifid' : notification [ 'campaign_id' ] , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# for i in range ( self . connection_error_retries ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# dry_run = ( ( 'dry_run' in notification ) and ( notification [ 'dry_run' ] == True ) ) #NEWLINE# response = self . gcm . json_request ( registration_ids = [ notification [ 'receiver_id' ] ] , data = data , time_to_live = expiry_seconds , retries = self . connection_error_retries , dry_run = dry_run ) #NEWLINE# if ( 'errors' in response ) : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_GCM_FATAL_ERROR #NEWLINE# for ( error , reg_id_array ) in response [ 'errors' ] . items ( ) : #NEWLINE# #INDENT# if ( len ( reg_id_array ) == 1 ) : #NEWLINE# #INDENT# reg_id = reg_id_array [ 0 ] #NEWLINE# if ( error == 'InvalidRegistration' ) : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_GCM_INVALID_REGISTRATION_ID #NEWLINE# self . log . error ( 'GCM InvalidRegistration for notification: {0}' . format ( notification ) ) #NEWLINE# #UNINDENT# if ( error == 'NotRegistered' ) : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_GCM_DEVICE_UNREGISTERED #NEWLINE# unregistered_data = { #NEWLINE# #INDENT# 'login_id' : notification [ 'login_id' ] , #NEWLINE# 'device_token' : error [ 'receiver_id' ] , #NEWLINE# #UNINDENT# } #NEWLINE# self . unregistered_devices . append ( unregistered_data ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( notification [ 'status' ] == const . NOTIFICATION_GCM_FATAL_ERROR ) : #NEWLINE# #INDENT# self . log . debug ( 'Undefined fatal error, notification: {0}, response: {1}' . format ( notification , response ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_SUCCESS #NEWLINE# #UNINDENT# if ( 'canonical' in response ) : #NEWLINE# #INDENT# self . log . debug ( 'GCM Canonical response: {0}' . format ( response ) ) #NEWLINE# for ( reg_id , canonical_id ) in response [ 'canonical' ] . items ( ) : #NEWLINE# #INDENT# canonical = { #NEWLINE# #INDENT# 'login_id' : notification [ 'login_id' ] , #NEWLINE# 'old_token' : reg_id , #NEWLINE# 'new_token' : canonical_id , #NEWLINE# #UNINDENT# } #NEWLINE# self . canonical_ids . append ( canonical ) #NEWLINE# #UNINDENT# #UNINDENT# break #NEWLINE# #UNINDENT# except GCMConnectionException as e : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_CONNECTION_ERROR #NEWLINE# self . log . warning ( 'GCM Connection error, failed in {0}th attempt' . format ( ( i + 1 ) ) ) #NEWLINE# if ( ( i + 1 ) < self . connection_error_retries ) : #NEWLINE# #INDENT# delay = ( 1 + ( i * 2 ) ) #NEWLINE# time . sleep ( delay ) #NEWLINE# #UNINDENT# #UNINDENT# except GCMUnavailableException as e : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_GCM_UNAVAILABLE #NEWLINE# self . log . warning ( 'GCM is unavailable, failed in {0}th attempt' . format ( ( i + 1 ) ) ) #NEWLINE# if ( ( i + 1 ) < self . connection_error_retries ) : #NEWLINE# #INDENT# delay = ( 5 + ( i * 2 ) ) #NEWLINE# time . sleep ( delay ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except GCMException as e : #NEWLINE# #INDENT# notification [ 'status' ] = const . NOTIFICATION_GCM_FATAL_ERROR #NEWLINE# self . log . error ( 'GCM Exception: "{0}"; while senidng notification: {1}' . format ( e , notification ) )