def test_full_payload(self): p = ua.TemplatePush(None) p.audience = ua.ios_channel('b8f9b663-0a3b-cf45-587a-be880946e881') p.device_types = ua.device_types('ios') p.merge_data = ua.merge_data( template_id='ef34a8d9-0ad7-491c-86b0-aea74da15161', substitutions={ 'FIRST_NAME': 'Bob', 'LAST_NAME': 'Smith', 'TITLE': '' } ) self.assertEqual( p.payload, { 'device_types': ['ios'], 'merge_data': { 'template_id': 'ef34a8d9-0ad7-491c-86b0-aea74da15161', 'substitutions': { 'FIRST_NAME': 'Bob', 'LAST_NAME': 'Smith', 'TITLE': '' } }, 'audience': { 'ios_channel': 'b8f9b663-0a3b-cf45-587a-be880946e881' } } )
def test_full_payload(self): p = ua.TemplatePush(None) p.audience = ua.ios_channel('b8f9b663-0a3b-cf45-587a-be880946e881') p.device_types = ua.device_types('ios') p.merge_data = ua.merge_data( template_id='ef34a8d9-0ad7-491c-86b0-aea74da15161', substitutions={ 'FIRST_NAME': 'Bob', 'LAST_NAME': 'Smith', 'TITLE': '' }) self.assertEqual( p.payload, { 'device_types': ['ios'], 'merge_data': { 'template_id': 'ef34a8d9-0ad7-491c-86b0-aea74da15161', 'substitutions': { 'FIRST_NAME': 'Bob', 'LAST_NAME': 'Smith', 'TITLE': '' } }, 'audience': { 'ios_channel': 'b8f9b663-0a3b-cf45-587a-be880946e881' } })
def test_scheduled_template(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/40fe5b31-8997-4819-9aeb-e6c4ae95e5d3' ] } ).encode('utf-8') response.status_code = 202 mock_request.return_value = response airship = ua.Airship(TEST_KEY, TEST_SECRET) sched = ua.ScheduledPush(airship) sched.schedule = ua.scheduled_time(datetime.datetime.now()) template_push = airship.create_template_push() template_push.audience = ua.ios_channel('780ba0c5-45be-4f29-befa-39135cb05b59') template_push.device_types = ua.device_types('ios') template_push.merge_data = ua.merge_data( template_id='780ba0c5-45be-4f29-befa-39135cb05b59', substitutions={'key': 'value'} ) sched.push = template_push sched.send() self.assertEqual( sched.url, 'https://go.urbanairship.com/api/schedules/40fe5b31-8997-4819-9aeb-e6c4ae95e5d3' )
def test_scheduled_template(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/40fe5b31-8997-4819-9aeb-e6c4ae95e5d3' ] }).encode('utf-8') response.status_code = 202 mock_request.return_value = response airship = ua.Airship(TEST_KEY, TEST_SECRET) sched = ua.ScheduledPush(airship) sched.schedule = ua.scheduled_time(datetime.datetime.now()) template_push = airship.create_template_push() template_push.audience = ua.ios_channel( '780ba0c5-45be-4f29-befa-39135cb05b59') template_push.device_types = ua.device_types('ios') template_push.merge_data = ua.merge_data( template_id='780ba0c5-45be-4f29-befa-39135cb05b59', substitutions={'key': 'value'}) sched.push = template_push sched.send() self.assertEqual( sched.url, 'https://go.urbanairship.com/api/schedules/40fe5b31-8997-4819-9aeb-e6c4ae95e5d3' )
def publish(user, publication): """Send notifaction to followers on publication""" followers = user.followers.all() for follower in followers: airship = ua.Airship(settings.URBANAIRSHIP_KEY, settings.URBANAIRSHIP_MASTER_SECRET) push = airship.create_push() push.notification = ua.notification(ios=ua.ios( alert="New publication from {}".format(user.username), badge='+1', extra={'url': publication.pk}, )) push.device_types = ua.device_types('ios') devices = models.Device.objects.filter(profile=follower.profile) if devices.exists(): device = devices[0] if device.chanid and device.chanid != "{}": push.audience = ua.ios_channel(device.chanid) push.send()
def push_notification(devices: list, title: str, message: str, extra: dict): push = airship.create_push() push.device_types = ua.device_types(*DeviceType.choices_list()) channels = [ua.android_channel(device.channel_id) for device in devices if device.device_type == 'android'] + \ [ua.ios_channel(device.channel_id) for device in devices if device.device_type == 'ios'] if len(channels) == 0: return push.audience = ua.and_(*channels) push.notification = ua.notification( alert=message, android=ua.android(alert=message, title=title, extra={'payload': json.dumps(extra)}), ios=ua.ios(alert=message, title=title, extra={'payload': json.dumps(extra)})) # print(push.payload) response = push.send() print(response.ok) return response