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 make_push(self, request, queryset): from APNSWrapper import APNSNotificationWrapper, APNSNotification, APNSProperty import binascii wrapper = {} for msg in queryset: if msg.sent: continue if msg.device.development: pem = msg.app.cert_dev else: pem = msg.app.cert_dist key = "%d_%d" % (msg.app.pk, msg.device.development) if not wrapper.has_key(key): wrapper[key] = APNSNotificationWrapper( "/home/mrgaolei/%s" % pem, msg.device.development) #wrapper = APNSNotificationWrapper("/home/mrgaolei/%s" % pem, msg.device.development) message = APNSNotification() message.token(binascii.unhexlify(msg.device.devtoken)) message.alert(msg.alert.encode("UTF-8")) message.badge(int(msg.badge)) message.sound(msg.sound) # property for ppt in msg.property_set.all(): message.appendProperty( APNSProperty(str(ppt.argkey), str(ppt.argvalue))) wrapper[key].append(message) #if wrapper.notify(): msg.sent = True msg.save() keys = wrapper.keys() for key in keys: wrapper[key].notify()
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 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
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 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!")
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()
def __init__(self, certificate, devel, token): self.apns = APNSNotificationWrapper(certificate, sandbox=devel) self.msg = APNSNotification() self.msg.token(token)
#!/usr/bin/env python from APNSWrapper import APNSNotificationWrapper, APNSNotification import binascii #apnstoken = '1c8cf15acb17f8362322ccff0452417dcd3f6b538193099e0347efb84e8a4a4f' # kevin #apnstoken = 'f02e7b4c384e32404645443203dd0b71750e54fe13b5d0a8a434a12a0f5e7a25' # bart #apnstoken = '8b78c702f8c8d5e02c925146d07c28f615283bc862b226343f013b5f8765ba5a' # travis # apnstoken = '01c386bff1c7b576629c3e4e3b101181d3acdfdb0947ba4ac5e513fb59775fc7' # mike #apnstoken = '48e3496a9fdadcc8ce8fe99020bb461a72bd0e20e1a8ee0878494133f8c23d4c' #landon apnstoken = '9d943f189085639bd87bac6c9dd2b89f237bee1e232041d4057f7a422f23deeb' #landon apnstoken = 'df687e03345604f6b02a4c32bc7d5220ddd5f832c270645e06f22cc26f66516a' # Kevin # create wrapper #wrapper = APNSNotificationWrapper('apns-dev.pem', True) wrapper = APNSNotificationWrapper('apns-ether-prod.pem', sandbox=False) # wrapper = APNSNotificationWrapper('apns-prod.pem', sandbox=False) # create message message = APNSNotification() message.token(binascii.unhexlify(apnstoken)) message.alert('Test') message.badge(1) # add message to tuple and send it to APNS server wrapper.append(message) wrapper.notify()