def test_ios_unicode(self): self.assertEqual( ua.notification( ios=ua.ios( alert=u'Hello', badge=u'+1', expiry=u'time', ) ), { 'ios': { 'alert': 'Hello', 'badge': '+1', 'expiry': 'time' } } ) self.assertEqual( ua.notification( ios=ua.ios( content_available=True ) ), { 'ios': { 'content-available': True } } )
def test_wns_payload(self): self.assertEqual(ua.notification(wns=ua.wns_payload(alert='Hello', )), {'wns': { 'alert': 'Hello', }}) self.assertEqual( ua.notification(wns=ua.wns_payload(toast={'key': 'value'}, )), {'wns': { 'toast': { 'key': 'value' }, }}) self.assertEqual( ua.notification(wns=ua.wns_payload(tile={'key': 'value'}, )), {'wns': { 'tile': { 'key': 'value' }, }}) self.assertEqual( ua.notification(wns=ua.wns_payload(badge={'key': 'Hello'}, )), {'wns': { 'badge': { 'key': 'Hello' }, }}) self.assertRaises(ValueError, ua.wns_payload, alert='Hello', tile='Foo')
def test_blackberry(self): self.assertEqual( ua.notification( blackberry=ua.blackberry( alert='Hello', ) ), { 'blackberry': { 'body': 'Hello', 'content_type': 'text/plain', } } ) self.assertEqual( ua.notification( blackberry=ua.blackberry( body='Hello', content_type='text/html', ) ), { 'blackberry': { 'body': 'Hello', 'content_type': 'text/html', } } ) self.assertRaises( ValueError, ua.blackberry, body='Hello' )
def test_wns_payload(self): self.assertEqual( ua.notification(wns=ua.wns_payload( alert='Hello', )), {'wns': { 'alert': 'Hello', }}) self.assertEqual( ua.notification(wns=ua.wns_payload( toast={'key': 'value'}, )), {'wns': { 'toast': {'key': 'value'}, }}) self.assertEqual( ua.notification(wns=ua.wns_payload( tile={'key': 'value'}, )), {'wns': { 'tile': {'key': 'value'}, }}) self.assertEqual( ua.notification(wns=ua.wns_payload( badge={'key': 'Hello'}, )), {'wns': { 'badge': {'key': 'Hello'}, }}) self.assertRaises(ValueError, ua.wns_payload, alert='Hello', tile='Foo')
def setUp(self): self.airship = ua.Airship(TEST_KEY, TEST_SECRET) self.name = "Experiment Test" self.audience = "all" self.description = "just testing" self.device_types = ["ios", "android"] self.campaigns = ua.campaigns(categories=["campaign", "categories"]) self.operation_id = "d67d4de6-934f-4ebb-aef0-250d89699b6b" self.experiment_id = "f0c975e4-c01a-436b-92a0-2a360f87b211" self.push_id = "0edb9e6f-2198-4c42-aada-5a49eb03bcbb" push_1 = self.airship.create_push() push_1.notification = ua.notification(alert="test message 1") push_2 = self.airship.create_push() push_2.notification = ua.notification(alert="test message 2") push_3 = self.airship.create_push() push_3.notification = ua.notification(alert="test message 1") push_3.in_app = ua.in_app(alert="This part appears in-app!", display_type="banner") push_4 = self.airship.create_push() push_4.notification = ua.notification(alert="test message 2") push_4.in_app = ua.in_app(alert="This part appears in-app!", display_type="banner", expiry="2025-10-14T12:00:00", display={"position": "top"}, actions={"add_tag": "in-app"} ) variant_1 = ua.Variant(push_1, description="A description of the variant", name="Testing") variant_2 = ua.Variant(push_2) variant_3 = ua.Variant(push_3, description="A description of the variant one", name="Testing", schedule=ua.scheduled_time( datetime.datetime(2025, 10, 10, 18, 45, 30)), weight=2 ) variant_4 = ua.Variant(push_4, description="A description of the variant two", name="Testing", schedule=ua.scheduled_time(datetime.datetime( 2025, 10, 10, 18, 45, 30)), weight=3 ) self.variants_1 = [variant_1, variant_2] self.variants_2 = [variant_3, variant_4]
def test_full_scheduled_payload(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification(alert='Hello') p.options = {} p.device_types = ua.all_ p.message = ua.message("Title", "Body", "text/html", "utf8") sched = ua.ScheduledPush(None) sched.push = p sched.name = "a schedule" sched.schedule = ua.scheduled_time( datetime.datetime(2014, 1, 1, 12, 0, 0)) self.assertEqual( sched.payload, { "name": "a schedule", "schedule": { 'scheduled_time': '2014-01-01T12:00:00' }, "push": { "audience": "all", "notification": { "alert": "Hello" }, "device_types": "all", "options": {}, "message": { "title": "Title", "body": "Body", "content_type": "text/html", "content_encoding": "utf8", }, } })
def test_web_push(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='Hello', web={ 'title': 'This is a title.', 'icon': {'url': 'https://example.com/icon.png'}, 'extra': {'attribute': 'id'}, 'time_to_live': 12345, 'require_interaction': False } ) p.device_types = 'web' self.assertEqual( p.payload, { 'audience': 'all', 'device_types': 'web', 'notification': { 'alert': 'Hello', 'web': { 'title': 'This is a title.', 'icon': {'url': 'https://example.com/icon.png'}, 'extra': {'attribute': 'id'}, 'time_to_live': 12345, 'require_interaction': False }, } } )
def test_local_schedule_success(self): with mock.patch.object(ua.Airship, '_request') as mock_request: response = requests.Response() response._content = json.dumps( { 'schedule_urls': [ ( 'https://go.urbanairship.com/api/schedules/' '0492662a-1b52-4343-a1f9-c6b0c72931c0' ) ] } ).encode('utf-8') response.status_code = 202 mock_request.return_value = response airship = ua.Airship('key', 'secret') sched = ua.ScheduledPush(airship) push = airship.create_push() push.audience = ua.all_ push.notification = ua.notification(alert='Hello') push.device_types = ua.all_ sched.push = push sched.schedule = ua.local_scheduled_time(datetime.datetime.now()) sched.send() self.assertEquals( sched.url, ( 'https://go.urbanairship.com/api/schedules/' '0492662a-1b52-4343-a1f9-c6b0c72931c0' ) )
def create_notification(receiver, reporter, content_object, notification_type): # If the receiver of this notification is the same as the reporter or # if the user has blocked this type, then don't create if receiver == reporter or not NotificationSetting.objects.get( notification_type=notification_type, user=receiver).allow: return notification = Notification.objects.create( user=receiver, reporter=reporter, content_object=content_object, notification_type=notification_type) notification.save() if AirshipToken.objects.filter(user=receiver, expired=False).exists(): try: device_tokens = list( AirshipToken.objects.filter( user=receiver, expired=False).values_list('token', flat=True)) airship = urbanairship.Airship(settings.AIRSHIP_APP_KEY, settings.AIRSHIP_APP_MASTER_SECRET) for device_token in device_tokens: push = airship.create_push() push.audience = urbanairship.device_token(device_token) push.notification = urbanairship.notification( ios=urbanairship.ios(alert=notification.push_message(), badge='+1')) push.device_types = urbanairship.device_types('ios') push.send() except urbanairship.AirshipFailure: pass
def test_standard_ios_opts(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='Top level alert', ios = ua.ios( alert='iOS override alert', sound='cat.caf', ) ) p.device_types = ua.device_types('ios') self.assertEqual( p.payload, { 'audience': 'all', 'device_types': ['ios'], 'notification': { 'alert': 'Top level alert', 'ios': { 'alert': 'iOS override alert', 'sound': 'cat.caf' } } } )
def create_notification(receiver, reporter, content_object, notification_type): # If the receiver of this notification is the same as the reporter or # if the user has blocked this type, then don't create if receiver == reporter or not NotificationSetting.objects.get(notification_type=notification_type, user=receiver).allow: return notification = Notification.objects.create(user=receiver, reporter=reporter, content_object=content_object, notification_type=notification_type) notification.save() if AirshipToken.objects.filter(user=receiver, expired=False).exists(): try: device_tokens = list(AirshipToken.objects.filter(user=receiver, expired=False).values_list('token', flat=True)) airship = urbanairship.Airship(settings.AIRSHIP_APP_KEY, settings.AIRSHIP_APP_MASTER_SECRET) for device_token in device_tokens: push = airship.create_push() push.audience = urbanairship.device_token(device_token) push.notification = urbanairship.notification(ios=urbanairship.ios(alert=notification.push_message(), badge='+1')) push.device_types = urbanairship.device_types('ios') push.send() except urbanairship.AirshipFailure: pass
def test_update_schedule(self): airship = ua.Airship('key', 'secret') sched = ua.ScheduledPush(airship) # Fail w/o URL self.assertRaises(ValueError, sched.update) with mock.patch.object(ua.Airship, '_request') as mock_request: url = ('https://go.urbanairship.com/api/schedules/' '0492662a-1b52-4343-a1f9-c6b0c72931c0') response = requests.Response() response.status_code = 202 response._content = json.dumps({ 'schedule_urls': [('https://go.urbanairship.com/api/schedules/' '0492662a-1b52-4343-a1f9-c6b0c72931c0')] }).encode('utf-8') mock_request.return_value = response sched.url = url push = airship.create_push() push.audience = ua.all_ push.notification = ua.notification(alert='Hello') push.device_types = ua.all_ sched.push = push sched.schedule = ua.scheduled_time(datetime.datetime.now()) sched.update()
def test_email_overrides(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( email=ua.email(message_type='transactional', plaintext_body='hello', reply_to='*****@*****.**', sender_address='*****@*****.**', sender_name='test_name', subject='hi', html_body='<html>so rich!</html>')) p.device_types = ua.device_types('email') self.assertEqual( p.payload, { 'audience': 'all', 'device_types': ['email'], 'notification': { 'email': { 'message_type': 'transactional', 'plaintext_body': 'hello', 'reply_to': '*****@*****.**', 'sender_address': '*****@*****.**', 'sender_name': 'test_name', 'subject': 'hi', 'html_body': '<html>so rich!</html>' } } })
def test_sms_overrides(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='top level alert', sms=ua.sms( alert='sms override alert', expiry='2018-04-01T12:00:00', ) ) p.device_types = ua.device_types('sms') self.assertEqual( p.payload, { 'audience': 'all', 'device_types': ['sms'], 'notification': { 'alert': 'top level alert', 'sms': { 'alert': 'sms override alert', 'expiry': '2018-04-01T12:00:00' } } } )
def test_full_scheduled_payload(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification(alert='Hello') p.options = {} p.device_types = ua.all_ p.message = ua.message("Title", "Body", "text/html", "utf8") sched = ua.ScheduledPush(None) sched.push = p sched.name = "a schedule" sched.schedule = ua.scheduled_time( datetime.datetime(2014, 1, 1, 12, 0, 0)) self.assertEqual(sched.payload, { "name": "a schedule", "schedule": {'scheduled_time': '2014-01-01T12:00:00'}, "push": { "audience": "all", "notification": {"alert": "Hello"}, "device_types": "all", "options": {}, "message": { "title": "Title", "body": "Body", "content_type": "text/html", "content_encoding": "utf8", }, } })
def notification(test): utcnow = datetime.datetime.utcnow() timestamp = utcnow.strftime('%Y-%m-%d %H:%M') notification = test['notification'] notification_opts = {} if 'actions' in test: notification_opts['actions'] = get_actions(test) if 'interactive' in test: notification_opts['interactive'] = get_interactive(test) if 'alert' in notification: notification_opts['alert'] = '%s\n%s' % (notification['alert'], timestamp) if 'ios' in notification: notification_opts['ios'] = get_ios_platform_opts(notification, timestamp) if 'android' in notification: notification_opts['android'] = get_android_platform_opts(notification, timestamp) return ua.notification(**notification_opts)
def test_update_schedule(self): airship = ua.Airship('key', 'secret') sched = ua.ScheduledPush(airship) # Fail w/o URL self.assertRaises(ValueError, sched.update) with mock.patch.object(ua.Airship, '_request') as mock_request: url = "https://go.urbanairship.com/api/schedules/0492662a-1b52-4343-a1f9-c6b0c72931c0" response = requests.Response() response.status_code = 202 response._content = ( '''{"schedule_urls": ["https://go.urbanairship.com/api/schedules/0492662a-1b52-4343-a1f9-c6b0c72931c0"]}''') mock_request.return_value = response sched.url = url push = airship.create_push() push.audience = ua.all_ push.notification = ua.notification(alert='Hello') push.device_types = ua.all_ sched.push = push sched.schedule = ua.scheduled_time(datetime.datetime.now()) sched.update()
def test_ios_unicode(self): self.assertEqual( ua.notification(ios=ua.ios( alert=u'Hello', badge=u'+1', expiry=u'time', )), {'ios': { 'alert': 'Hello', 'badge': '+1', 'expiry': 'time' }}) self.assertEqual(ua.notification(ios=ua.ios(content_available=True)), {'ios': { 'content-available': True }})
def test_ios_alert_dict(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification(ios=ua.ios(alert={'foo': 'bar'})) p.options = ua.options(10080) p.device_types = 'ios' p.message = ua.message( title='Title', body='Body', content_type='text/html', content_encoding='utf8', ) self.assertEqual( p.payload, { 'audience': 'all', 'notification': { 'ios': { 'alert': { 'foo': 'bar' } } }, 'device_types': 'ios', 'options': { 'expiry': 10080 }, 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', } })
def options(self): airship = ua.Airship('key', 'secret') push = ua.Push(None) push.audience = ua.all_ push.notification = ua.notification(alert='Hello Expiry') push.options = ua.options(expiry='2013-04-01T18:45:0') push.device_types = ua.all_
def test_email_overrides(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( email=ua.email( message_type='transactional', plaintext_body='hello', reply_to='*****@*****.**', sender_address='*****@*****.**', sender_name='test_name', subject='hi', html_body='<html>so rich!</html>' ) ) p.device_types = ua.device_types('email') self.assertEqual( p.payload, { 'audience': 'all', 'device_types': ['email'], 'notification': { 'email': { 'message_type': 'transactional', 'plaintext_body': 'hello', 'reply_to': '*****@*****.**', 'sender_address': '*****@*****.**', 'sender_name': 'test_name', 'subject': 'hi', 'html_body': '<html>so rich!</html>' } } } )
def push(self, notification): if not config.get(config.SERVICES_ENABLED, 'off') == 'on': logger.info('notifications disabled: %s marked as pending' % notification.id) return push = self.airship.create_push() push.audience = self.make_tags(notification.tags) push.notification = ua.notification( ios=ua.ios(alert=notification.message, extra=notification.payload)) push.device_types = ua.device_types('ios') if notification.scheduled_for: schedule = self.airship.create_scheduled_push() schedule.push = push schedule.name = notification.type if notification.scheduled_for_local: schedule.schedule = local_scheduled_time( notification.scheduled_for) else: schedule.schedule = ua.scheduled_time( notification.scheduled_for) logger.info("Sending scheduled push to Urban Airship") resp = schedule.send() else: logger.info("Sending push to Urban Airship") resp = push.send() notification.meta['ua_response'] = resp.payload notification.sent = True
def push(self, notification): if not config.get(config.SERVICES_ENABLED, 'off') == 'on': logger.info('notifications disabled: %s marked as pending' % notification.id) return push = self.airship.create_push() push.audience = self.make_tags(notification.tags) push.notification = ua.notification(ios=ua.ios(alert=notification.message, extra=notification.payload)) push.device_types = ua.device_types('ios') if notification.scheduled_for: schedule = self.airship.create_scheduled_push() schedule.push = push schedule.name = notification.type if notification.scheduled_for_local: schedule.schedule = local_scheduled_time(notification.scheduled_for) else: schedule.schedule = ua.scheduled_time(notification.scheduled_for) logger.info("Sending scheduled push to Urban Airship") resp = schedule.send() else: logger.info("Sending push to Urban Airship") resp = push.send() notification.meta['ua_response'] = resp.payload notification.sent = True
def test_sms_overrides(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='top level alert', sms = ua.sms( alert='sms override alert', expiry='2018-04-01T12:00:00', ) ) p.device_types = ua.device_types('sms') self.assertEqual( p.payload, { 'audience': 'all', 'device_types': ['sms'], 'notification': { 'alert': 'top level alert', 'sms': { 'alert': 'sms override alert', 'expiry': '2018-04-01T12:00:00' } } } )
def test_standard_ios_opts(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='Top level alert', ios=ua.ios( alert='iOS override alert', sound='cat.caf', ) ) p.device_types = ua.device_types('ios') self.assertEqual( p.payload, { 'audience': 'all', 'device_types': ['ios'], 'notification': { 'alert': 'Top level alert', 'ios': { 'alert': 'iOS override alert', 'sound': 'cat.caf' } } } )
def test_email_missing_override(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification(alert='no email to be found!') p.device_types = ua.device_types('email') with self.assertRaises(ValueError): p.send()
def test_interactive(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='Hey, click yes!', interactive=ua.interactive( type='some_type', button_actions={ 'yes': { 'add_tag': 'clicked_yes', 'remove_tag': 'never_clicked_yes', 'open': { 'type': 'url', 'content': 'http://www.urbanairship.com' } }, 'no': { 'add_tag': 'hater' } })) p.device_types = ua.all_ p.message = ua.message( title='Title', body='Body', content_type='text/html', content_encoding='utf8', ) self.assertEqual( p.payload, { 'audience': 'all', 'notification': { 'alert': 'Hey, click yes!', 'interactive': { 'type': 'some_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', 'remove_tag': 'never_clicked_yes', 'open': { 'type': 'url', 'content': 'http://www.urbanairship.com' } }, 'no': { 'add_tag': 'hater' } } } }, 'device_types': 'all', 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', } })
def push_message_for_user(self, message, user, status): #self.airship.push({'aps': {'alert': message, 'badge':1, 'sound': 'default'}, # 'status':status}, aliases=[user.username]) push = self.airship.create_push() push.audience = ua.or_(ua.alias(user.username)) push.notification = ua.notification(alert=message) push.device_types = ua.device_types('ios', 'android') push.send()
def test_amazon_unicode(self): self.assertEqual( ua.notification(amazon=ua.amazon( alert=u'Amazon test', expires_after=u'100', )), {'amazon': { 'alert': 'Amazon test', 'expires_after': '100', }})
def send_push_notification(): push = airship.create_push() push.audience = ua.all_ push.notification = ua.notification( alert="Betimlenecek bir gorsel var", ios=ios(sound='betim', badge=1), ) push.device_types = ua.all_ push.send()
def test_blackberry(self): self.assertEqual( ua.notification(blackberry=ua.blackberry(alert='Hello', )), {'blackberry': { 'body': 'Hello', 'content_type': 'text/plain', }}) self.assertEqual( ua.notification(blackberry=ua.blackberry( body='Hello', content_type='text/html', )), {'blackberry': { 'body': 'Hello', 'content_type': 'text/html', }}) self.assertRaises(ValueError, ua.blackberry, body='Hello')
def test_android_unicode(self): self.assertEqual( ua.notification(android=ua.android( alert=u'Hello', time_to_live=u'100', )), {'android': { 'alert': 'Hello', 'time_to_live': '100', }})
def test_simple_alert(self): self.assertEqual( ua.notification( alert='Hello' ), { 'alert': 'Hello' } )
def test_amazon(self): self.assertEqual( ua.notification( amazon=ua.amazon(alert='Amazon test', title='My Title', consolidation_key='123456', expires_after=100, summary='Summary of message', extra={'more': 'stuff'}, interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } }, style={ 'type': 'big_picture', 'big_picture': 'bigpic.png', 'title': 'A title', 'summary': 'A summary' }, sound='cowbell.mp3')), { 'amazon': { 'alert': 'Amazon test', 'title': 'My Title', 'consolidation_key': '123456', 'expires_after': 100, 'summary': 'Summary of message', 'extra': { 'more': 'stuff', }, 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } }, 'style': { 'type': 'big_picture', 'big_picture': 'bigpic.png', 'title': 'A title', 'summary': 'A summary' }, 'sound': 'cowbell.mp3' } })
def test_email_missing_override(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='no email to be found!' ) p.device_types = ua.device_types('email') with self.assertRaises(ValueError): p.send()
def test_full_scheduled_payload(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification(alert='Hello') p.options = ua.options(10080) p.device_types = ua.all_ p.message = ua.message( title='Title', body='Body', content_type='text/html', content_encoding='utf8', extra={'more': 'stuff'}, expiry=10080, icons={'list_icon': 'http://cdn.example.com/message.png'}, options={'some_delivery_option': 'true'}, ) sched = ua.ScheduledPush(None) sched.push = p sched.name = 'a schedule' sched.schedule = ua.scheduled_time( datetime.datetime(2014, 1, 1, 12, 0, 0)) self.assertEqual( sched.payload, { 'name': 'a schedule', 'schedule': { 'scheduled_time': '2014-01-01T12:00:00' }, 'push': { 'audience': 'all', 'notification': { 'alert': 'Hello' }, 'device_types': 'all', 'options': { 'expiry': 10080 }, 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', 'extra': { 'more': 'stuff' }, 'expiry': 10080, 'icons': { 'list_icon': 'http://cdn.example.com/message.png' }, 'options': { 'some_delivery_option': 'true' }, }, } })
def test_ios(self): self.assertEqual( ua.notification(ios=ua.ios(alert='Hello', badge='+1', sound='cat.caf', extra={'more': 'stuff'})), { 'ios': { 'alert': 'Hello', 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', } } }) self.assertEqual(ua.notification(ios=ua.ios(content_available=True)), {'ios': { 'content-available': True }})
def test_email_send(self): cas = ua.CreateAndSendPush( airship=self.airship, channels=self.test_email_objs ) cas.notification = ua.notification( email=ua.email( message_type='commercial', plaintext_body='this is an email', reply_to='*****@*****.**', sender_address='*****@*****.**', sender_name='test sender', subject='this is an email' ) ) cas.device_types = ua.device_types('email') cas.campaigns = ua.campaigns( categories=['email', 'fun'] ) self.assertEqual( cas.payload, { 'audience': { 'create_and_send': [ { 'ua_address': '*****@*****.**', 'ua_commercial_opted_in': '2018-02-13T11:58:59' }, { 'ua_address': '*****@*****.**', 'ua_commercial_opted_in': '2018-02-13T11:58:59' }, { 'ua_address': '*****@*****.**', 'ua_commercial_opted_in': '2018-02-13T11:58:59' } ] }, 'device_types': ['email'], 'notification': { 'email': { 'subject': 'this is an email', 'plaintext_body': 'this is an email', 'message_type': 'commercial', 'sender_name': 'test sender', 'sender_address': '*****@*****.**', 'reply_to': '*****@*****.**' } }, 'campaigns': { 'categories': ['email', 'fun'] } } )
def test_ios(self): self.assertEqual( ua.notification(ios=ua.ios( alert='Hello', badge='+1', sound='cat.caf', extra={'more': 'stuff'} )), {'ios': { 'alert': 'Hello', 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', } }}) self.assertEqual( ua.notification(ios=ua.ios(content_available=True)), {'ios': { 'content-available': True}})
def test_scheduled_send(self): cas = ua.CreateAndSendPush( airship=self.airship, channels=self.test_sms_objs ) cas.notification = ua.notification( alert='test sms' ) cas.device_types = ua.device_types('sms') cas.campaigns = ua.campaigns( categories=['sms', 'offers'] ) schedule = ua.ScheduledPush(airship=self.airship) schedule.name = 'test schedule name' schedule.push = cas schedule.schedule = ua.scheduled_time( datetime.datetime(2025, 10, 8, 12, 15) ) self.assertEqual( schedule.payload, { 'schedule': { 'scheduled_time': '2025-10-08T12:15:00' }, 'name': 'test schedule name', 'push': { 'audience': { 'create_and_send': [ { 'ua_msisdn': '15035556789', 'ua_sender': '12345', 'ua_opted_in': '2018-02-13T11:58:59' }, { 'ua_msisdn': '15035556788', 'ua_sender': '12345', 'ua_opted_in': '2018-02-13T11:58:59' }, { 'ua_msisdn': '15035556787', 'ua_sender': '12345', 'ua_opted_in': '2018-02-13T11:58:59' }, ] }, 'notification': {'alert': 'test sms'}, 'device_types': ['sms'], 'campaigns': { 'categories': ['sms', 'offers'] } } } )
def apns_send(): message = 'Hello world2' send_notification(message) push = airship.create_push() push.audience = ua.all_ push.notification = ua.notification(alert=message) push.device_types = ua.all_ push.send() # try: # except: # return 'error' return 'success'
def test_full_scheduled_payload(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification(alert='Hello') p.options = ua.options(10080) p.device_types = ua.all_ p.message = ua.message( title='Title', body='Body', content_type='text/html', content_encoding='utf8', extra={'more': 'stuff'}, expiry=10080, icons={ 'list_icon': 'http://cdn.example.com/message.png' }, options={'some_delivery_option': 'true'}, ) sched = ua.ScheduledPush(None) sched.push = p sched.name = 'a schedule' sched.schedule = ua.scheduled_time( datetime.datetime(2014, 1, 1, 12, 0, 0) ) self.assertEqual( sched.payload, { 'name': 'a schedule', 'schedule': {'scheduled_time': '2014-01-01T12:00:00'}, 'push': { 'audience': 'all', 'notification': {'alert': 'Hello'}, 'device_types': 'all', 'options': { 'expiry': 10080 }, 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', 'extra': {'more': 'stuff'}, 'expiry': 10080, 'icons': { 'list_icon': 'http://cdn.example.com/message.png' }, 'options': {'some_delivery_option': 'true'}, }, } } )
def test_actions(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( alert='Hello', actions=ua.actions( add_tag='new_tag', remove_tag='old_tag', share='Check out Urban Airship!', open_={ 'type': 'url', 'content': 'http://www.urbanairship.com' }, app_defined={'some_app_defined_action': 'some_values'} ) ) p.device_types = ua.all_ p.message = ua.message( title='Title', body='Body', content_type='text/html', content_encoding='utf8', ) self.assertEqual( p.payload, { 'audience': 'all', 'notification': { 'alert': 'Hello', 'actions': { 'add_tag': 'new_tag', 'remove_tag': 'old_tag', 'share': 'Check out Urban Airship!', 'open': { 'type': 'url', 'content': 'http://www.urbanairship.com' }, 'app_defined': { 'some_app_defined_action': 'some_values' } } }, 'device_types': 'all', 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', } } )
class test_error_response(unittest.TestCase): test_channel = str(uuid.uuid4()) airship = ua.Airship(TEST_KEY, TEST_SECRET) common_push = airship.create_push() common_push.device_types = ua.device_types('ios', 'android', 'amazon') common_push.audience = ua.channel(test_channel) common_push.notification = ua.notification(alert='testing') def test_unauthorized(self): with mock.patch.object(ua.Airship, '_request') as mock_request: response = requests.Response() response._content = json.dumps({ 'ok': False }).encode('utf-8') response.status_code = 401 mock_request.return_value = response try: self.common_push.send() except Exception as e: self.assertIsInstance(ua.Unauthorized, e) def test_client_error(self): with mock.patch.object(ua.Airship, '_request') as mock_request: response = requests.Response() response._content = json.dumps({ 'ok': False }).encode('utf-8') response.status_code = 400 mock_request.return_value = response try: r = self.common_push.send() except Exception as e: self.assertIsInstance(ua.AirshipFailure, e) self.assertEqual(r.status_code, 400) def test_server_error(self): with mock.patch.object(ua.Airship, '_request') as mock_request: response = requests.Response() response._content = json.dumps({ 'ok': False }).encode('utf-8') response.status_code = 500 mock_request.return_value = response try: r = self.common_push.send() except Exception as e: self.assertIsInstance(ua.AirshipFailure, e) self.assertEqual(r.status_code, 500)
def send_push_notification(receiver, message): if AirshipToken.objects.filter(user=receiver, expired=False).exists(): try: device_tokens = list(AirshipToken.objects.filter(user=receiver, expired=False).values_list('token', flat=True)) airship = urbanairship.Airship(settings.AIRSHIP_APP_KEY, settings.AIRSHIP_APP_MASTER_SECRET) for device_token in device_tokens: push = airship.create_push() push.audience = urbanairship.device_token(device_token) push.notification = urbanairship.notification(ios=urbanairship.ios(alert=message, badge='+1')) push.device_types = urbanairship.device_types('ios') push.send() except urbanairship.AirshipFailure: pass
def test_sms_inline_template(self): cas = ua.CreateAndSendPush( airship=self.airship, channels=self.test_sms_objs ) cas.notification = ua.notification(sms=ua.sms( template_alert=self.template_alert )) cas.device_types = ua.device_types('sms') self.assertEqual( cas.payload, { 'audience': { 'create_and_send': [ { 'ua_msisdn': '15035556789', 'ua_sender': '12345', 'ua_opted_in': '2018-02-13T11:58:59', 'name': 'bruce', 'event': 'zoom meeting' }, { 'ua_msisdn': '15035556788', 'ua_sender': '12345', 'ua_opted_in': '2018-02-13T11:58:59', 'name': 'bruce', 'event': 'zoom meeting' }, { 'ua_msisdn': '15035556787', 'ua_sender': '12345', 'ua_opted_in': '2018-02-13T11:58:59', 'name': 'bruce', 'event': 'zoom meeting' }, ] }, 'notification': { 'sms': { 'template': { 'fields': { 'alert': '{{name}} you are late for your {{event}}' } } } }, 'device_types': ['sms'] } )
def test_email_with_device_type_all(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( email=ua.email(message_type='transactional', plaintext_body='hello', reply_to='*****@*****.**', sender_address='*****@*****.**', sender_name='test_name', subject='hi', html_body='<html>so rich!</html>')) p.device_types = ua.all_ with self.assertRaises(ValueError): p.send()
def pushNotification(self, resp, no_update_list): '''push notification''' resp.response.out.write("<br/>Start Pushing Notification <br/>") airship = ua.Airship('LTjlWamyTzyBHhVmzMLu_A','OB3h24o3RYOan5-JQWdVGQ') push = airship.create_push() push.device_types = ua.device_types('ios','android') # if len(no_update_list) == 0: # push.audience = ua.tag('260') # push.notification = ua.notification(alert="FAWN TESTING") for data in no_update_list: message = """%s is offline with FAWN for 2 hours.""" %(data[2]) resp.response.out.write(message) push.audience = ua.tag(data[0]) push.notification = ua.notification(alert = message) push.send()
def test_amazon_unicode(self): self.assertEqual( ua.notification( amazon=ua.amazon( alert=u'Amazon test', expires_after=u'100', ) ), { 'amazon': { 'alert': 'Amazon test', 'expires_after': '100', } } )
def test_android_unicode(self): self.assertEqual( ua.notification( android=ua.android( alert=u'Hello', time_to_live=u'100', ) ), { 'android': { 'alert': 'Hello', 'time_to_live': '100', } } )
def test_amazon(self): self.assertEqual( ua.notification( amazon=ua.amazon( alert='Amazon test', title='My Title', consolidation_key='123456', expires_after=100, summary='Summary of message', extra={'more': 'stuff'}, interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } } ) ), { 'amazon': { 'alert': 'Amazon test', 'title': 'My Title', 'consolidation_key': '123456', 'expires_after': 100, 'summary': 'Summary of message', 'extra': { 'more': 'stuff', }, 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } } } } )
def test_mixed_platforms(self): email_channel = ua.Email( self.airship, address='*****@*****.**', commercial_opted_in=self.test_optin_datestring) mixed_channels = self.test_sms_objs mixed_channels.append(email_channel) cas = ua.CreateAndSendPush( self.airship, channels=mixed_channels ) cas.device_types = ua.device_types('sms') cas.notification = ua.notification(alert='test sms') with self.assertRaises(TypeError): cas.payload