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 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_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 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_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 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 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_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_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 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 send_notification(message): now = datetime.datetime.now() timestamp = int(time.mktime(now.timetuple())) store.add_notification(Notification(str(uuid.uuid4()), message, 'hoge.com', timestamp, 'http://image.com')) try: push = airship.create_push() push.audience = ua.all_ notification = ua.ios(alert=message, extra={'url': 'https://google.com'}) push.ios = notification push.device_types = ua.ios push.send() except: return False return True
def create_notification(type, source_id, from_user, for_user, message=None): notification = Notification(type=type, source_id=source_id, from_user=from_user, for_user=for_user, message=message, seen=False, action_taken=False) notification.save() extra = { "id": notification.id, "type": notification.type } app_key = settings.UA['app_key'] master_secret = settings.UA['master_secret'] logger.debug("app_key") logger.debug(app_key) logger.debug("master_secret") logger.debug(master_secret) airship = ua.Airship(app_key, master_secret) devices = for_user.user_devices.all() for device in devices: logger.debug("push_token") logger.debug(device.push_token) if device.push_token: push = airship.create_push() if device.device_type == 'iOS': push.audience = ua.device_token(device.push_token) elif device.device_type == 'Android': push.audience = ua.apid(device.push_token) push.notification = ua.notification(ios=ua.ios(alert=message, badge="+1", extra=extra), android=ua.android(alert=message, extra=extra)) push.device_types = ua.device_types('ios', 'android', ) logger.debug("push response") logger.debug(push.send()) return notification
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 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 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
def test_ios_overrides(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( ios=ua.ios( alert={'title': 'this is', 'body': 'backwards', 'summary-arg': 'Matmos', 'summary-arg-count': 1}, sound={'name': 'Amplified Synapse', 'volume': 0.8, 'critical': False}, thread_id='plastic minor', priority=10, badge=3, extra={'office': 'furniture'}, mutable_content=False, title='this is', subtitle='backwards', collapse_id='nugent sand' ) ) 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': { 'title': 'this is', 'body': 'backwards', 'summary-arg': 'Matmos', 'summary-arg-count': 1 }, 'sound': { 'name': 'Amplified Synapse', 'volume': 0.8, 'critical': False }, 'thread_id': 'plastic minor', 'priority': 10, 'badge': 3, 'extra': { 'office': 'furniture' }, 'mutable_content': False, 'title': 'this is', 'subtitle': 'backwards', 'collapse_id': 'nugent sand' } }, 'device_types': 'ios', 'options': { 'expiry': 10080 }, 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', } } )
def test_ios(self): self.assertEqual( ua.notification( ios=ua.ios( alert='Hello', badge='+1', sound='cat.caf', extra={'more': 'stuff'}, expiry='time', category='test', title='title', interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', }, 'no': { 'add_tag': 'clicked_no' } } } ) ), { 'ios': { 'alert': 'Hello', 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', }, 'expiry': 'time', 'category': 'test', 'title': 'title', 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', }, 'no': { 'add_tag': 'clicked_no' } } } } } ) self.assertEqual( ua.notification( ios=ua.ios( alert={ 'foo': 'bar' }, badge='+1', sound='cat.caf', extra={ 'more': 'stuff' }, category='test', interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } }, ) ), { 'ios': { 'alert': {'foo': 'bar'}, 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', }, 'category': 'test', 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } } } } ) self.assertEqual( ua.notification( ios=ua.ios( content_available=True ) ), { 'ios': { 'content-available': True} } )
def test_ios_overrides(self): p = ua.Push(None) p.audience = ua.all_ p.notification = ua.notification( ios=ua.ios(alert={ 'title': 'this is', 'body': 'backwards', 'summary-arg': 'Matmos', 'summary-arg-count': 1 }, sound={ 'name': 'Amplified Synapse', 'volume': 0.8, 'critical': False }, thread_id='plastic minor', priority=10, badge=3, extra={'office': 'furniture'}, mutable_content=False, title='this is', subtitle='backwards', collapse_id='nugent sand')) 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': { 'title': 'this is', 'body': 'backwards', 'summary-arg': 'Matmos', 'summary-arg-count': 1 }, 'sound': { 'name': 'Amplified Synapse', 'volume': 0.8, 'critical': False }, 'thread_id': 'plastic minor', 'priority': 10, 'badge': 3, 'extra': { 'office': 'furniture' }, 'mutable_content': False, 'title': 'this is', 'subtitle': 'backwards', 'collapse_id': 'nugent sand' } }, 'device_types': 'ios', 'options': { 'expiry': 10080 }, 'message': { 'title': 'Title', 'body': 'Body', 'content_type': 'text/html', 'content_encoding': 'utf8', } })
def test_ios(self): self.assertEqual( ua.notification(ios=ua.ios(alert='Hello', badge='+1', sound='cat.caf', extra={'more': 'stuff'}, expiry='time', category='test', title='title', interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', }, 'no': { 'add_tag': 'clicked_no' } } })), { 'ios': { 'alert': 'Hello', 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', }, 'expiry': 'time', 'category': 'test', 'title': 'title', 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', }, 'no': { 'add_tag': 'clicked_no' } } } } }) self.assertEqual( ua.notification(ios=ua.ios( alert={'foo': 'bar'}, badge='+1', sound='cat.caf', extra={'more': 'stuff'}, category='test', interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } }, )), { 'ios': { 'alert': { 'foo': 'bar' }, 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', }, 'category': 'test', 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } } } }) self.assertEqual(ua.notification(ios=ua.ios(content_available=True)), {'ios': { 'content-available': True }})
def test_ios(self): self.assertEqual( ua.notification(ios=ua.ios( alert='Hello', badge='+1', sound='cat.caf', extra={'more': 'stuff'}, expiry='time', category='test', title='title', subtitle='Subtitle', mutable_content=True, media_attachment={ 'content': { 'title': 'Moustache Twirl', 'body': 'Have you ever seen a moustache like this?!' }, 'options': { 'crop': { 'height': 0.5, 'width': 0.5, 'x': 0.25, 'y': 0.25 }, 'time': 15 }, 'url': 'https://media.giphy.com/media/JYsWwF82EGnpC/giphy.gif' }, interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', }, 'no': { 'add_tag': 'clicked_no' } } }, priority=10, collapse_id='a')), { 'ios': { 'alert': 'Hello', 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', }, 'expiry': 'time', 'category': 'test', 'title': 'title', 'subtitle': 'Subtitle', 'mutable_content': True, 'media_attachment': { 'content': { 'title': 'Moustache Twirl', 'body': 'Have you ever seen a moustache like this?!' }, 'options': { 'crop': { 'height': 0.5, 'width': 0.5, 'x': 0.25, 'y': 0.25 }, 'time': 15 }, 'url': 'https://media.giphy.com/media/JYsWwF82EGnpC/giphy.gif' }, 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes', }, 'no': { 'add_tag': 'clicked_no' } } }, 'priority': 10, 'collapse_id': 'a' } }) self.assertEqual( ua.notification(ios=ua.ios( alert={'foo': 'bar'}, badge='+1', sound='cat.caf', extra={'more': 'stuff'}, category='test', interactive={ 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } }, )), { 'ios': { 'alert': { 'foo': 'bar' }, 'badge': '+1', 'sound': 'cat.caf', 'extra': { 'more': 'stuff', }, 'category': 'test', 'interactive': { 'type': 'a_type', 'button_actions': { 'yes': { 'add_tag': 'clicked_yes' }, 'no': { 'add_tag': 'clicked_no' } } } } }) self.assertEqual(ua.notification(ios=ua.ios(content_available=True)), {'ios': { 'content-available': True }})
def push(self, notification): pyld_json = json.dumps( ua.notification(ios=ua.ios(alert=notification.message, extra=notification.context))) self.payloads.append(pyld_json)
def push(self, notification): pyld_json = json.dumps(ua.notification(ios=ua.ios(alert=notification.message, extra=notification.context))) self.payloads.append(pyld_json)