def _push(self, _apns, _body, _user_config): _ios_token = _user_config.get("device_ios_token") if _ios_token == None or len(_ios_token) == 0: return _user_language = _user_config.get("user_language") or "en_US" _title = push_title(_body.get("mt"), _body.get("ms"), _body.get("bo"), _user_language) _sound = None if not _user_config.get("user_silence_notification"): _sound = "beep.wav" _count = _user_config.get("unacked_notification_count") _m = {"alert": _title, "sound": _sound, "badge": _count} from apns2.client import APNsClient from apns2.payload import Payload _dev = _user_config.get("is_development") _client = APNsClient(_apns.get("combination_pem"), use_sandbox=False, use_alternative_port=False) if _dev != None and _dev == True: _client = APNsClient(_apns.get("combination_pem"), use_sandbox=True, use_alternative_port=False) _payload = Payload(**_m) _client.send_notification(_ios_token, _payload) return
def update_card(): print('start update card') users = User.objects.all() for user in users: if user.card_drawer_of_next_day != '10102347454878415': user.update(card_drawer=None, draw_card_status='undraw') user.card_drawer = user.card_drawer_of_next_day user.card_drawer_of_next_day = None user.save() # send push notification print('send notification') try: user_list = User.objects.all() for user in user_list: if user.card_drawer != '10102347454878415': if user.device.ios != '': custom_payload = {'type': 'card'} payload = Payload(alert='已經有新的卡片囉!', sound="default", badge=1, custom=custom_payload) client = APNsClient(APNS_CERT, use_sandbox=USE_SANDBOX, use_alternative_port=False) client.send_notification(user.device.ios, payload, APNS_TOPIC) print('succeed') except Exception as e: print(e)
def send_Notification( message, key): token_hex = 'e4d3e8cb6e8c29d0dd6af4926e698c69632e2a8965cd17d66ed2d0cdd7869269' payload = Payload(alert="Hello World!", sound="default", badge=1) topic = 'ASU.SeatsFinderBot' client = APNsClient(key, use_sandbox=True, use_alternative_port=False) client.send_notification(token_hex, payload, topic)
def test_send_notification_failure_410(): with patch("apns2.credentials.CertificateCredentials.create_connection" ) as create_connection_mock: http_client_mock = MagicMock() http_client_mock.post.return_value = Response(status_code=410, json={ "reason": "BadDeviceToken", "timestamp": time.time() }) create_connection_mock.return_value = http_client_mock client = APNsClient(CertificateCredentials("test/mycert.pem")) exception_raised = False try: client.send_notification( token_hex=TOKEN, notification=Payload(alert="Test alert"), topic=TOPIC, ) except BadDeviceToken: exception_raised = True http_client_mock.post.assert_called_once() assert exception_raised is True
def __init__(self, sandbox=True, yaml_tokens=False): self.sandbox = sandbox self.yaml = yaml_tokens self.payload = None self.tokens = [] with open("app.yaml") as app_file: try: app_data = yaml.safe_load(app_file) except yaml.YAMLError: sys.exit(yaml.YAMLError) else: self.config = { "auth-key": app_data["auth-key"], "auth-key-filename": app_data["auth-key-filename"], "bundle-id": app_data["bundle-id"], "cert-filename": app_data["cert-filename"], "team-id": app_data["team-id"], "api-url": app_data["api-url"] + "/tokens/" + app_data["bundle-id"] } self.credentials = TokenCredentials(auth_key_path=self.config["auth-key-filename"], auth_key_id=self.config["auth-key"], team_id=self.config["team-id"]) self.client = APNsClient(credentials=self.credentials, use_sandbox=self.sandbox) self.load_tokens()
def send_Notification(self, message, key, device_id): token_hex = device_id payload = Payload(alert=message, sound="default", badge=1) topic = 'ASU.SeatsFinderBot' client = APNsClient(key, use_sandbox=False, use_alternative_port=False) client.send_notification(token_hex, payload, topic)
def send_ios(self, story, user, usersub): if not self.is_ios: return tokens = MUserNotificationTokens.get_tokens_for_user(self.user_id) # To update APNS: # 1. Create certificate signing requeswt in Keychain Access # 2. Upload to https://developer.apple.com/account/resources/certificates/list # 3. Download to secrets/certificates/ios/aps.cer # 4. Open in Keychain Access and export as aps.p12 # 4. Export private key as aps_key.p12 WITH A PASSPHRASE (removed later) # 5. openssl pkcs12 -in aps.p12 -out aps.pem -nodes -clcerts -nokeys # 6. openssl pkcs12 -clcerts -nokeys -out aps.pem -in aps.p12 # 7. cat aps.pem aps_key.noenc.pem > aps.p12.pem # 8. Verify: openssl s_client -connect gateway.push.apple.com:2195 -cert aps.p12.pem # 9. Deploy: aps -l work -t apns,repo,celery apns = APNsClient( '/srv/newsblur/config/certificates/aps.p12.pem', use_sandbox=tokens.use_sandbox ) notification_title_only = is_true(user.profile.preference_value('notification_title_only')) title, subtitle, body = self.title_and_body(story, usersub, notification_title_only) image_url = None if len(story['image_urls']): image_url = story['image_urls'][0] # print image_url confirmed_ios_tokens = [] for token in tokens.ios_tokens: logging.user( user, '~BMStory notification by iOS: ~FY~SB%s~SN~BM~FY/~SB%s' % (story['story_title'][:50], usersub.feed.feed_title[:50]), ) payload = Payload( alert={'title': title, 'subtitle': subtitle, 'body': body}, category="STORY_CATEGORY", mutable_content=True, custom={ 'story_hash': story['story_hash'], 'story_feed_id': story['story_feed_id'], 'image_url': image_url, }, ) try: apns.send_notification(token, payload, topic="com.newsblur.NewsBlur") except (BadDeviceToken, Unregistered): logging.user(user, '~BMiOS token expired: ~FR~SB%s' % (token[:50])) else: confirmed_ios_tokens.append(token) if settings.DEBUG: logging.user( user, '~BMiOS token good: ~FB~SB%s / %s' % (token[:50], len(confirmed_ios_tokens)), ) if len(confirmed_ios_tokens) < len(tokens.ios_tokens): tokens.ios_tokens = confirmed_ios_tokens tokens.save()
def __init__(self): self.config = Config() self.topic = 'BEPco.chatter' useSandbox = not self.config.isProd() cert = 'certs/sandbox.pem' if useSandbox else 'certs/prod.pem' self.client = APNsClient(cert, use_sandbox=useSandbox, use_alternative_port=False)
def get_apns_client() -> APNsClient: global _apns_client if _apns_client is None: # NB if called concurrently, this will make excess connections. # That's a little sloppy, but harmless unless a server gets # hammered with a ton of these all at once after startup. _apns_client = APNsClient(credentials=settings.APNS_CERT_FILE, use_sandbox=settings.APNS_SANDBOX) return _apns_client
def connect(self) -> bool: try: self.client = APNsClient(credentials=self.credentials, use_sandbox=self.use_sandbox, use_alternative_port=self.use_alternative_port, proto=self.proto, json_encoder=self.json_encoder, password=self.password, proxy_host=self.proxy_host, proxy_port=self.proxy_port) return True except IOError as error: self.error('failed to connect apple server: %s' % error) return False
def send_apn(push_token: str): ''' Sends an empty APN to the given device push_token ''' payload = Payload() client = APNsClient(config.PASS_TYPE_CERTIFICATE_PATH, use_sandbox=False, use_alternative_port=False, password=config.PEM_PASSWORD) client.send_notification(push_token, payload, config.PASS_TYPE_IDENTIFIER)
def send_message(self, message=None, **kwargs): """Send push message to registered devices.""" apns = APNsClient( self.certificate, use_sandbox=self.sandbox, use_alternative_port=False ) device_state = kwargs.get(ATTR_TARGET) if (message_data := kwargs.get(ATTR_DATA)) is None: message_data = {}
def send_message(self, message=None, **kwargs): """Send push message to registered devices.""" from apns2.client import APNsClient from apns2.payload import Payload from apns2.errors import Unregistered apns = APNsClient(self.certificate, use_sandbox=self.sandbox, use_alternative_port=False) device_state = kwargs.get(ATTR_TARGET) message_data = kwargs.get(ATTR_DATA) if message_data is None: message_data = {} if isinstance(message, str): rendered_message = message elif isinstance(message, template_helper.Template): rendered_message = message.render() else: rendered_message = "" payload = Payload( alert=rendered_message, badge=message_data.get("badge"), sound=message_data.get("sound"), category=message_data.get("category"), custom=message_data.get("custom", {}), content_available=message_data.get("content_available", False), ) device_update = False for push_id, device in self.devices.items(): if not device.disabled: state = None if device.tracking_device_id is not None: state = self.device_states.get( device.full_tracking_device_id) if device_state is None or state == str(device_state): try: apns.send_notification(push_id, payload, topic=self.topic) except Unregistered: logging.error("Device %s has unregistered", push_id) device_update = True device.disable() if device_update: self.write_devices() return True
def send_notification(self, userId, message="Hello World!"): token_hex = self.get_token(userId) payload = Payload(alert=message, sound="default", badge=1) print(payload.__dict__) topic = 'com.epage.QuietMind' self.client = APNsClient(DEV_CERT_FILE, use_sandbox=True, use_alternative_port=False) self.client.send_notification(token_hex, payload, topic)
def __init__(self, topic, auth_key_path, auth_key_id, team_id, use_sandbox=False): self.topic = topic self.client = APNsClient(TokenCredentials(auth_key_path, auth_key_id, team_id), use_sandbox=use_sandbox, use_alternative_port=False)
def push(request): #fcm_device = GCMDevice.objects.create(registration_id="e--N1a4DuTg:APA91bGrwQnf8h5o3UHrVPx3PTkTVumOEfHP5dfAdvFDuBA2h6QZVG0MYuQtGRH21nvdnb44sThudENkAeEtrmBND3g1gv2A0IlLW4ZMsCGl_AsBDWCR4FJm6s57pf-PPz5BGTkIWCbw", cloud_message_type="FCM", user='******') #device = GCMDevice.objects.get(registration_id="e--N1a4DuTg:APA91bGrwQnf8h5o3UHrVPx3PTkTVumOEfHP5dfAdvFDuBA2h6QZVG0MYuQtGRH21nvdnb44sThudENkAeEtrmBND3g1gv2A0IlLW4ZMsCGl_AsBDWCR4FJm6s57pf-PPz5BGTkIWCbw") token_hex = '106ececed6c5985efb2afd75618a41120086e7e93488315dc03ccd9eff0be257' payload = Payload(alert="Hello World!", sound="default", badge=1) topic = 'com.example.App' client = APNsClient( 'https://s3-us-west-1.amazonaws.com/blake-deets/aps_development.cer', use_sandbox=False, use_alternative_port=False) client.send_notification(token_hex, payload, topic)
def sendpushnotification(DeviceToken, OrderID, StoreID, dev_flag, Message): #send the push notification custom = {'launchURL': 'x-com.petco.wrapper.sim://launch'} payload = Payload(alert=Message, sound="popcorn.wav", badge=0, custom=custom) topic = 'com.petco.notifications' IOS_Client = APNsClient('./Apple_Certificate/server1.pem', use_sandbox=dev_flag, use_alternative_port=False) IOS_Client.send_notification(DeviceToken, payload, topic) return True
def connect_apns_server(cls, sandbox, p12, secret): pub_key, priv_key = cls.gen_pem(p12, secret) f, path = tempfile.mkstemp() try: os.write(f, pub_key) os.write(f, priv_key) client = APNsClient(path, use_sandbox=sandbox, use_alternative_port=False) return client finally: os.close(f) os.remove(path)
def get_apns_client() -> Any: # We lazily do this import as part of optimizing Zulip's base # import time. from apns2.client import APNsClient global _apns_client, _apns_client_initialized if not _apns_client_initialized: # NB if called concurrently, this will make excess connections. # That's a little sloppy, but harmless unless a server gets # hammered with a ton of these all at once after startup. if settings.APNS_CERT_FILE is not None: _apns_client = APNsClient(credentials=settings.APNS_CERT_FILE, use_sandbox=settings.APNS_SANDBOX) _apns_client_initialized = True return _apns_client
def send_ios(self, story, user, usersub): if not self.is_ios: return tokens = MUserNotificationTokens.get_tokens_for_user(self.user_id) apns = APNsClient('/srv/newsblur/config/certificates/aps.p12.pem', use_sandbox=tokens.use_sandbox) notification_title_only = is_true( user.profile.preference_value('notification_title_only')) title, subtitle, body = self.title_and_body(story, usersub, notification_title_only) image_url = None if len(story['image_urls']): image_url = story['image_urls'][0] # print image_url confirmed_ios_tokens = [] for token in tokens.ios_tokens: logging.user( user, '~BMStory notification by iOS: ~FY~SB%s~SN~BM~FY/~SB%s' % (story['story_title'][:50], usersub.feed.feed_title[:50])) payload = Payload(alert={ 'title': title, 'subtitle': subtitle, 'body': body }, category="STORY_CATEGORY", mutable_content=True, custom={ 'story_hash': story['story_hash'], 'story_feed_id': story['story_feed_id'], 'image_url': image_url, }) try: apns.send_notification(token, payload, topic="com.newsblur.NewsBlur") except (BadDeviceToken, Unregistered): logging.user(user, '~BMiOS token expired: ~FR~SB%s' % (token[:50])) else: confirmed_ios_tokens.append(token) if settings.DEBUG: logging.user( user, '~BMiOS token good: ~FB~SB%s / %s' % (token[:50], len(confirmed_ios_tokens))) if len(confirmed_ios_tokens) < len(tokens.ios_tokens): tokens.ios_tokens = confirmed_ios_tokens tokens.save()
def send_apn_notification(message, endpoint): token = endpoint.device_token payload = Payload(alert=message, sound="default", badge=1) if settings.APNS_CERT_FILE: # allows for reuse of apns client and reuse of connections # TODO: handles errors by fcm client global apns_client if not apns_client: apns_client = APNsClient(settings.APNS_CERT_FILE, password=settings.APNS_CERT_PASSWORD, use_sandbox=settings.DEBUG, use_alternative_port=False) topic = settings.APNS_APP_BUNDLE_ID apns_client.send_notification(token, payload, topic)
def send_push(self, title, body): connects = UserConnectionField.objects.find_type_connection_by_user( self.user, UserConnectionField.APNS_TYPE) client = APNsClient(settings.APNS_KEY_LOCATION, use_sandbox=settings.APNS_SENDBOX, use_alternative_port=False) payload = Payload(alert=PayloadAlert(title=title, body=body), sound="default", badge=1) for connect in connects: client.send_notification(token_hex=connect.value, notification=payload, topic=settings.APNS_BUNDLE_ID)
def push(category, title, body, device_token, params): payload = Payload(alert=PayloadAlert(title=None if title == '' else title, body=body), sound='1107', badge=int(params['badge']) if 'badge' in params else 0, category=category, mutable_content=True, custom=params) client = APNsClient(CERT_PATH) try: client.send_notification(device_token, payload, 'me.fin.bark') return '' except APNsException as e: return str(e)
def test_send_notification_success(): with patch("apns2.credentials.CertificateCredentials.create_connection" ) as create_connection_mock: http_client_mock = MagicMock() http_client_mock.post.return_value = Mock(status_code=200) create_connection_mock.return_value = http_client_mock client = APNsClient(CertificateCredentials("test/mycert.pem")) client.send_notification( token_hex=TOKEN, notification=Payload(alert="Test alert"), topic=TOPIC, ) http_client_mock.post.assert_called_once()
def setUp(self): self.open_streams = 0 self.max_open_streams = 0 self.mock_results = None self.next_stream_id = 0 with patch('apns2.credentials.HTTP20Connection' ) as mock_connection_constructor, patch( 'apns2.credentials.init_context'): self.mock_connection = MagicMock() self.mock_connection.get_response.side_effect = self.mock_get_response self.mock_connection.request.side_effect = self.mock_request self.mock_connection._conn.__enter__.return_value = self.mock_connection._conn self.mock_connection._conn.remote_settings.max_concurrent_streams = 500 mock_connection_constructor.return_value = self.mock_connection self.client = APNsClient(credentials=None)
def apns_send(tokens, data): if current_app.config['TESTING'] is True: current_app.config['TESTING_APNS'].append(data) return payload = Payload(alert=data['service']['name'], sound="default", badge=1, custom=data) notifications = [ Notification(token=token, payload=payload) for token in tokens ] topic = 'me.elrod.iPushjet' client = APNsClient(apns_cert_path, use_sandbox=True, use_alternative_port=False) client.send_notification_batch(notifications, topic)
def send_notification(user_id, st_code, condition, curr_value): logger.info("User to notify: %s" % user_id) notif_flag = False # Get the real name of the station station_name = Station.query.filter(Station.code == st_code).one().name logger.info("Station to be notified about: %s" % station_name) # Get the notification token from the users table token_hex = User.query.filter(User.device_id == user_id).one().notif_token logger.info("Notification token: %s" % token_hex) # Get the key, key path and team_id from the constants file and build # token credentials object token_credentials = TokenCredentials(auth_key_path=NOTIF_AUTH_KEY_PATH, auth_key_id=NOTIF_AUTH_KEY_ID, team_id=NOTIF_TEAM_ID) # Compose the text notif_text = notif_texts_format[condition['dimension']][condition['quantifier']] % \ (station_name,condition['value'],float(curr_value)) logger.info("Sending notification: %s" % notif_text) # Compose the dict with the station code and station name custom_data = {'station_code': st_code, 'station_name': station_name} topic = NOTIF_TOPIC payload = Payload(alert=notif_text, sound="default", badge=0, custom=custom_data) client = APNsClient(credentials=token_credentials, use_sandbox=True) try: #apns.gateway_server.send_notification(token_hex, payload,identifier=identifier) client.send_notification(token_hex, payload, topic) mark_as_notified(user_id, st_code, condition) logger.info("Notification sent!") notif_flag = True except: logger.error("Error sending notification!!") print("ERROR ENVIANDO NOTIFICACION") return notif_flag
def push(): # test token = '9dsfeg2fa913b390fc6b3d3ec637a764867241f516d847b54d24ce7e37963b6e' token_hexs = [] for i in range(10): token_hexs.append(token) token_hexs.append('sss') payload = Payload(alert="Hello World!", sound="default") client = APNsClient( 'Dev_Push_Cer-key.pem', # 'Release_Push_Cer_Key.pem', use_sandbox=True, use_alternative_port=False) st = time.time() response = client.send_notification_multiple(token_hexs, payload) et = time.time() print(et - st) print(response)
def push_notification(self, credentials, push_sandbox, pass_type_id, push_token): payload = Payload() try: client = APNsClient( credentials, use_sandbox=push_sandbox, use_alternative_port=False, ) client.send_notification( push_token, payload, pass_type_id, ) except SSLError as e: logger.error("django_walletpass SSLError: %s", e) except StreamResetError as e: logger.error("django_walletpass StreamResetError. Bad cert or token? %s", e) # Errors should never pass silently. except Exception as e: # Unless explicitly silenced. logger.error("django_walletpass uncaught error %s", e)
def sendMultipleNotifications(tokens, title, body, flightString, dateString): costumDict = { "flightString": flightString, "dateString": dateString, } payload = Payload(alert=PayloadAlert(title=title, body=body), custom=costumDict, sound="default", badge=0) topic = 'com.MihaStrah.FlightTracker' token_credentials = TokenCredentials( auth_key_path="AppleAuthentication/AuthKey_85KZTANBJ8.p8", auth_key_id="85KZTANBJ8", team_id="7YNLV7443U") client = APNsClient(credentials=token_credentials, use_sandbox=False) Notification = collections.namedtuple('Notification', ['token', 'payload']) notifications = [] for token in tokens: logger.info("notification sent to APNS") notifications.append(Notification(payload=payload, token=token)) #pošljemo skupek obvestil (za vse uporabnike, ki so naročeni na let) client.send_notification_batch(notifications=notifications, topic=topic)