コード例 #1
0
ファイル: alerts.py プロジェクト: Stamped/Stamped
    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!")
コード例 #2
0
 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