class TestService(unittest.TestCase): def setUp(self): self.device_token = TEST_DEVICE_TOKEN self.notif = APNSNotification(device_token=self.device_token, alert="foobar", badge=1, sound="default") self.apns = APNS(ROOT_DIR + "/config/apns_cert.development.pem") self.apns.start() self.assertTrue(self.apns.wait_status(30)) def test_basic(self): print "Connected", self.apns.get_status() self.apns.stop() def test_send_notification(self): self.apns.put_notification(self.notif) time.sleep(1) def test_send_error_notification(self): self.notif.token = "abcd" self.apns.put_notification(self.notif) import time time.sleep(1) self.assertTrue(self.apns.get_last_error()) self.assertEqual(self.apns.get_last_error().status_code, 8) def test_feedback(self): item = self.apns.get_feedback(timeout=5) print item
@route('/push/') def index(): return ('<h1>Registered devices</h1>', '<br/>'.join('<strong>%s</strong>' % x for x in devices)) @put('/push/device/') def register_device(): devices.add(request.forms.token) return "ok" @post('/push/device/') def send_notification(): apns.put(request.forms.token, request.forms.msg) return 'ok' # start apn service CERTIFICATE = ROOT_DIR+'/config/apns_cert.development.pem' from apns import APNS logger.info("Connecting to APNs ...") apns = APNS(CERTIFICATE) apns.start() assert apns.wait_status(30) if __name__ == '__main__': run(server='gevent', host='0.0.0.0', port=8080, debug=DEBUG, reloader=False) else: application = default_app()