python - PyAPNs and the need to Sleep between Sends -
i using pyapns send notifications ios devices. sending groups of notifications @ once. if of tokens bad reason, process stop. result using enhanced setup , following method:
apns.gateway_server.register_response_listener
i use track token problem , pick there sending rest. issue when sending way trap these errors use sleep timer between token sends. example:
for x in self.retryapnlist: apns.gateway_server.send_notification(x, payload, identifier = token) time.sleep(0.5)
if don't use sleep timer no errors caught , entire apn list not sent process stops when there bad token. however, sleep timer arbitrary. .5 seconds enough while other times have had set 1. in no case has worked without sleep delay being added. doing slows down web calls , feels less bullet proof enter random sleep times.
any suggestions how can work without delay between apn calls or there best practice delay needed?
adding more code due request made below. here 3 methods inside of class use control this:
class pushadmin(webapp2.requesthandler): retryapnlist=[] channelid="" channelname = "" username="" apns = apns(use_sandbox=true,cert_file="mycert.pem", key_file="mykey.pem", enhanced=true) def devchannelpush(self,channel,name,sendalerts): ucs = usedchannelstore() pus = pushupdatestore() channelname = "" refreshapnlist = pus.getapn(channel) if sendalerts: alertapnlist,channelname = ucs.getapn(channel) if not alertapnlist: alertapnlist=[] if not refreshapnlist: refreshapnlist=[] pushapnlist = list(set(alertapnlist+refreshapnlist)) elif refreshapnlist: pushapnlist = refreshapnlist else: pushapnlist = [] self.retryapnlist = pushapnlist self.channelid = channel self.channelname = channelname self.username = name self.retryapnpush() def retryapnpush(self): token = -1 payload = payload(alert="a message " +self.username+ " posted "+self.channelname, sound="default", badge=1, custom={"channel":self.channelid}) if len(self.retryapnlist)>0: token +=1 x in self.retryapnlist: self.apns.gateway_server.send_notification(x, payload, identifier = token) time.sleep(0.5)
below calling class (abbreviate reduce non-related items):
class channelstore(ndb.model): def writemessage(self,id,name,message,imagekey,filekey): notify = pushadmin() notify.devchannelpush(id,name,true)
below slight change made placement of sleep timer seems have resolved issue. am, however, still concerned whether time given right amount in circumstances.
def retryapnpush(self): identifier = 1 token = -1 payload = payload(alert="a message " +self.username+ " posted "+self.channelname, sound="default", badge=1, custom={"channel":self.channelid}) if len(self.retryapnlist)>0: token +=1 x in self.retryapnlist: self.apns.gateway_server.send_notification(x, payload, identifier = token) time.sleep(0.5)
resolution:
as noted in comments @ bottom, resolution problem move following statement module level outside class. doing there no need sleep statements.
apns = apns(use_sandbox=true,cert_file="mycert.pem", key_file="mykey.pem", enhanced=true)
in fact, pyapns auto resend dropped notifications you, please see pyapns
so don't have retry yourself, can record notifications have bad tokens.
the behavior of code might result apns object kept in local scope (within if len(self.retryapnlist)>0:
)
i suggest pull out apns object class or module level, can complete error handling procedure , reuse tcp connection.
please kindly let me know if helps, :)
Comments
Post a Comment