def testAPNSWrapper(encoded_token, cert_path='iphone_cert.pem', sandbox=True): cert_path = 'iphone_cert.pem' """ Method to testing apns-wrapper module. """ wrapper = APNSNotificationWrapper(cert_path, sandbox=sandbox, debug_ssl=True, force_ssl_command=False) badge(wrapper, encoded_token) sound(wrapper, encoded_token) alert(wrapper, encoded_token) wrapper.connect() wrapper.notify() wrapper.disconnect() feedback = APNSFeedbackWrapper(cert_path, sandbox=sandbox, debug_ssl=True, force_ssl_command=False) feedback.receive() print "\n".join(["> " + base64.standard_b64encode(y) for x, y in feedback])
def send_notification(self, alert): #deviceToken = 'e328c75878c5b9566080482cb7bc56015b59faf69c8363325326314cadcdda9b' deviceTokenUnHex = binascii.unhexlify(self.device_id) wrapper = APNSNotificationWrapper(settings.PROJECT_ROOT + '/NotificationCombined.pem', False) message = APNSNotification() message.token(deviceTokenUnHex) message.alert(alert.encode("utf-8")) message.badge(0) message.sound('default') wrapper.append(message) wrapper.notify()
def sendPush(self, noop=False): logs.info("Submitting push notifications to %s devices" % len(self._pushQueue)) # Apply rate limit limit = 3 for deviceId, pushQueue in self._pushQueue.iteritems(): if IS_PROD or deviceId in self._adminTokens: count = 0 apns_wrapper = APNSNotificationWrapper(self._apnsCert, sandbox=self._sandbox) pushQueue.reverse() for notification in pushQueue: count += 1 if count > limit: logs.debug("Limit exceeded for device '%s'" % deviceId) break try: # Build APNS notification logs.debug("Push activityId '%s' to device '%s'" % (notification.activityId, deviceId)) apnsNotification = APNSNotification() apnsNotification.token(binascii.unhexlify(deviceId)) if notification.message is not None: msg = notification.message.encode('ascii', 'ignore') apnsNotification.alert(msg) if notification.badge is not None: apnsNotification.badge(notification.badge) # Add notification to wrapper apns_wrapper.append(apnsNotification) except Exception as e: logs.warning("Push failed: '%s'" % notification) logs.warning(utils.getFormattedException()) if apns_wrapper.count() > 0: if not noop: apns_wrapper.notify() logs.info("Success!")
class Notification: def __init__(self, certificate, devel, token): self.apns = APNSNotificationWrapper(certificate, sandbox=devel) self.msg = APNSNotification() self.msg.token(token) def badgeupdate(self, badge): self.msg.badge(badge) def alertmessage(self, message): self.msg.alert(message) # alert = APNSAlert() # alert.body(message) # alert.loc_key("ALERTMSG") # alert.action_loc_key("OPEN") # m.alert(alert) def send(self): self.apns.append(self.msg) self.apns.notify();
def testAPNSWrapper(encoded_token, cert_path, sandbox=True): """ Method to testing apns-wrapper module. """ wrapper = APNSNotificationWrapper(cert_path, sandbox=sandbox, debug_ssl=True, force_ssl_command=False) badge(wrapper, encoded_token) sound(wrapper, encoded_token) alert(wrapper, encoded_token) wrapper.connect() wrapper.notify() wrapper.disconnect() feedback = APNSFeedbackWrapper(cert_path, sandbox=sandbox, debug_ssl=True, force_ssl_command=False) feedback.receive() print "\n".join(["> " + base64.standard_b64encode(y) for x, y in feedback])
def taskThreadProc(self): self.logger.warning('%s started'%threading.currentThread().getName()) apnsWrapper = APNSNotificationWrapper(self.pemfilename, self.sandbox) while self.status != self.WORK_STOP: self.logger.debug('%s : apnsWrapper.count()=%d'%(threading.currentThread().getName(),apnsWrapper.count())) if apnsWrapper.count() == 0: try: taskstr = self.tashQueue.get(timeout=10) except Queue.Empty,e: self.logger.info('%s Get 0 task in 10s'%(threading.currentThread().getName())) continue else: try: taskstr = self.tashQueue.get(timeout=1) except Queue.Empty,e: self.logger.info('%s is to notify %d push'%(threading.currentThread().getName(),apnsWrapper.count())) result = apnsWrapper.notify() self.logger.info('%s notified %d push:%s'%(threading.currentThread().getName(),apnsWrapper.count(),result)) apnsWrapper = APNSNotificationWrapper(self.pemfilename, self.sandbox) continue
def send_alert(alert_text, token, cert, isDev=False, extra_args=[]): wrapper = APNSNotificationWrapper(cert, isDev) message = create_message(token, alert_text, extra_args=extra_args) wrapper.append(message) wrapper.notify()
def send_messages(messages, cert, isDev=False): wrapper = APNSNotificationWrapper(cert, isDev) for message in messages: wrapper.append(message) wrapper.notify()
def send_apple_push_notification(token, text, badge=None, sound=None, data=None): logger.debug("Sending PUSH Notification to [%s]..." % token) try: # create wrapper wrapper = APNSNotificationWrapper(settings.APNS_CERT, sandbox=settings.APNS_SANDBOX) # create message message = APNSNotification() logger.debug("Token used: [%s]" % token) message.token(binascii.unhexlify(token)) logger.debug("create alert...") # create custom alert message alert = APNSAlert() logger.debug("Encoded text [%s]" % (text.encode("ASCII", 'ignore'))) # add body to alert alert.body(text.encode("ASCII", 'ignore')) if data: logger.debug("\t\tAdding data") # here is argument *arg1* with integer value *54* for key in list(data.keys()): if isinstance(key, str): #print "Key: %s" % key ekey = key.encode("ASCII", 'ignore') else: ekey = "%s" % key #print "EKey: %s" % ekey #print " : %s" % data[key] if isinstance(data[key], str): apns_data = APNSProperty( ekey, data[key].encode("ASCII", 'ignore')) else: apns_data = APNSProperty(ekey, data[key]) # append properties to notification #print apns_data message.appendProperty(apns_data) message.alert(alert) if badge: logger.debug("\tBadge: [%s]" % badge) message.badge(badge) if sound: logger.debug("\tSound") message.sound() logger.debug("\tAdding message...") # add message to tuple and send it to APNS server wrapper.append(message) logger.debug("\tSending...") # On Stage the following errors for some reason wrapper.notify() logger.debug("\tSent!") return True except Exception: LogTraceback() return False
def send_apple_push_notification( token, text, badge=None, sound=None, data=None ): logger.debug( "Sending PUSH Notification to [%s]..." % token ) try: # create wrapper wrapper = APNSNotificationWrapper( settings.APNS_CERT, sandbox=settings.APNS_SANDBOX ) # create message message = APNSNotification() logger.debug( "Token used: [%s]" % token ) message.token( binascii.unhexlify( token ) ) logger.debug( "create alert..." ) # create custom alert message alert = APNSAlert() logger.debug( "Encoded text [%s]" % ( text.encode( "ASCII", 'ignore' ) ) ) # add body to alert alert.body( text.encode( "ASCII", 'ignore' ) ) if data: logger.debug( "\t\tAdding data" ) # here is argument *arg1* with integer value *54* for key in data.keys(): if isinstance( key, str ): #print "Key: %s" % key ekey = key.encode( "ASCII", 'ignore' ) else: ekey = "%s" % key #print "EKey: %s" % ekey #print " : %s" % data[key] if isinstance( data[key], str ): apns_data = APNSProperty( ekey, data[key].encode( "ASCII", 'ignore' ) ) else: apns_data = APNSProperty( ekey, data[key] ) # append properties to notification #print apns_data message.appendProperty( apns_data ) message.alert(alert) if badge: logger.debug( "\tBadge: [%s]" % badge ) message.badge( badge ) if sound: logger.debug( "\tSound" ) message.sound() logger.debug( "\tAdding message..." ) # add message to tuple and send it to APNS server wrapper.append( message ) logger.debug( "\tSending..." ) # On Stage the following errors for some reason wrapper.notify() logger.debug( "\tSent!" ) return True except Exception: LogTraceback() return False
import sys from APNSWrapper.connection import APNSServiceConnection from APNSWrapper import APNSNotificationWrapper, APNSNotification, APNSAlert, APNSProperty encoded_token = 'PIpQK61TJ55KuCIYIzhgMiD40t+PR8o4y/0FRoB5GAE=' try: encoded_token = sys.argv[1] except IndexError: pass connection = APNSServiceConnection(host='127.0.0.1', port=1025) wrapper = APNSNotificationWrapper(None, connection=connection) message = APNSNotification() message.tokenBase64(encoded_token) message.badge(7) message.sound('basso') wrapper.append(payload=message) wrapper.notify()