def enviar_notificacion_onesignal(self, notificacion): ajuste = self.db.query(Ajuste).first() print("Enviando Notificacion a un solo cliente") try: # Parametros APP_ID = ajuste.app_id REST_API_KEY = ajuste.rest_api_key CHANNEL_ID = ajuste.channel_id # DE LA CATEGORIA PRIORITARY cli = notificacion.receptor.token_notificacion if cli and cli != 'undefined' and cli != '0': print('Token..:') print(cli) client = Client(app_id=APP_ID, rest_api_key=REST_API_KEY) img = '' notification_body = { 'contents': {'en': notificacion.mensaje, 'es': notificacion.mensaje}, # 'subtitle': {'en': n.subtitle, 'es': n.subtitle}, // Si se quiere agregar un subtitulo 'headings': {'en': notificacion.titulo, 'es': notificacion.titulo}, # 'included_segments': ['Active Users', 'Inactive Users'], // cuando se especifica el include_player_ids, los segmentos ya no se envian 'include_player_ids': [cli], 'big_picture': img, # Foto cuando la notificacion se expande 'small_icon': 'icon', # Icono de la notificacion # 'android_accent_color': '0065ab',# Color de fondo del small_icon # 'huawei_accent_color': '0065ab',# Color de fondo del small_icon 'huawei_small_icon': 'icon', 'large_icon': img, # Foto con la notificacion sin expandirse 'huawei_large_icon': img, # Foto con la notificacion sin expandirse 'android_channel_id': CHANNEL_ID, # Categoria de la notificacion definida en OneSignal 'huawei_channel_id': CHANNEL_ID, # Categoria de la notificacion definida en OneSignal 'android_background_layout': '{"headings_color": "FFFF0000", "contents_color": "FF00FF00"}' } print('Cuerpo de la Notificacion..') print('..') print('..') print(notification_body) print('..') print('..') response = client.send_notification(notification_body) # print('Notificacion enviada: ') # print(response) # self.deshabilitar_notificacion(n) except OneSignalHTTPError as e: print("Error al enviar la notificacion: " + str(notificacion.id) + ' ' + str(notificacion.titulo)) print(e) print(e.message)
def test_view_apps(self, client: Client, ok_response: MockHttpxResponse): with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.USER_AUTH_TOKEN, expected_method='GET', expected_in_path='/apps')): response = client.view_apps() assert response.status_code == 200 assert response.body['success']
def test_create_app(self, client: Client, ok_response: MockHttpxResponse): body = {'name': 'new-app', 'chrome_key': 'secret-key'} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.USER_AUTH_TOKEN, expected_method='POST', expected_in_path='/apps', required_body=body)): response = client.create_app(body) assert response.status_code == 200 assert response.body['success']
def test_delete_segment(self, client: Client, ok_response: MockHttpxResponse): segment_id = '0bb44ff' with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='DELETE', expected_in_path= f'/apps/{self.APP_ID}/segments/{segment_id}')): response = client.delete_segment(segment_id) assert response.status_code == 200 assert response.body['success']
def test_create_segment(self, client: Client, ok_response: MockHttpxResponse): body = {'name': 'new segment'} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='POST', expected_in_path=f'/apps/{self.APP_ID}/segments', required_body=body)): response = client.create_segment(body) assert response.status_code == 200 assert response.body['success']
def test_update_app(self, client: Client, ok_response: MockHttpxResponse): body = {'name': 'new-name'} app_id = '0fff11' with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.USER_AUTH_TOKEN, expected_method='PUT', expected_in_path=f'/apps/{app_id}', required_body=body)): response = client.update_app(app_id, body) assert response.status_code == 200 assert response.body['success']
def test_view_device(self, client: Client, ok_response: MockHttpxResponse): device_id = 'player1' with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='GET', expected_in_path=f'/players/{device_id}', required_params={ 'app_id': self.APP_ID, })): response = client.view_device(device_id) assert response.status_code == 200 assert response.body['success']
def test_edit_tags(self, client: Client, ok_response: MockHttpxResponse): user_id = 'some-user' body = {'tags': {'rank': ''}} with mock.patch( 'httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='PUT', expected_in_path=f'/apps/{self.APP_ID}/users/{user_id}', required_body=body)): response = client.edit_tags(user_id, body) assert response.status_code == 200 assert response.body['success']
def test_add_device(self, client: Client, ok_response: MockHttpxResponse): body = {'device_type': 2} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='POST', expected_in_path='/players', required_body={ 'app_id': self.APP_ID, **body, })): response = client.add_device(body) assert response.status_code == 200 assert response.body['success']
def test_view_outcomes(self, client: Client, ok_response: MockHttpxResponse): outcome_names = ['foo', 'bar', 'halt'] with mock.patch( 'httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='GET', expected_in_path=f'/apps/{self.APP_ID}/outcomes', required_params={'outcome_names': ','.join(outcome_names)})): response = client.view_outcomes(outcome_names) assert response.status_code == 200 assert response.body['success']
def test_csv_export(self, client: Client, ok_response: MockHttpxResponse): body = {'last_active_since': '1469392779'} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='POST', expected_in_path='/players/csv_export', required_params={ 'app_id': self.APP_ID, }, required_body=body)): response = client.csv_export(body) assert response.status_code == 200 assert response.body['success']
def test_view_notifications(self, client: Client, ok_response: MockHttpxResponse): query = {'limit': 4, 'offset': 10} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='GET', expected_in_path='/notifications', required_params={ 'app_id': self.APP_ID, **query, })): response = client.view_notifications(query) assert response.status_code == 200 assert response.body['success']
def test_view_notification(self, client: Client, ok_response: MockHttpxResponse): notification_id = 'notification-one-id' with mock.patch( 'httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='GET', expected_in_path=f'/notifications/{notification_id}', required_params={ 'app_id': self.APP_ID, })): response = client.view_notification(notification_id) assert response.status_code == 200 assert response.body['success']
def test_edit_device(self, client: Client, ok_response: MockHttpxResponse): device_id = 'player1' body = {'language': 'ch'} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='PUT', expected_in_path=f'/players/{device_id}', required_body={ 'app_id': self.APP_ID, **body, })): response = client.edit_device(device_id, body) assert response.status_code == 200 assert response.body['success']
def test_send_notification(self, client: Client, ok_response: MockHttpxResponse): body = {'contents': {'en': 'hey there'}} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='POST', expected_in_path='/notification', required_body={ 'app_id': self.APP_ID, **body, })): response = client.send_notification(body) assert response.status_code == 200 assert response.body['success']
def test_new_session(self, client: Client, ok_response: MockHttpxResponse): device_id = 'player0' body = {'game_version': '1.2'} with mock.patch( 'httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='POST', expected_in_path=f'/players/{device_id}/on_session', required_body={ 'app_id': self.APP_ID, **body, })): response = client.new_session(device_id, body) assert response.status_code == 200 assert response.body['success']
def test_notification_history(self, client: Client, ok_response: MockHttpxResponse): notification_id = 'notification-one-id' body = {'events': 'clicked', 'email': '*****@*****.**'} with mock.patch('httpx.request', side_effect=mock_request( response=ok_response, expected_auth_token=self.REST_API_KEY, expected_method='POST', expected_in_path= f'/notifications/{notification_id}/history', required_body={ 'app_id': self.APP_ID, **body, })): response = client.notification_history(notification_id, body) assert response.status_code == 200 assert response.body['success']
from django.conf import settings from onesignal_sdk.client import Client from onesignal_sdk.error import OneSignalHTTPError ONESIGNAL_APP_ID = settings.ONESIGNAL_APP_ID ONESIGNAL_REST_API_KEY = settings.ONESIGNAL_REST_API_KEY onesignal_client = Client(app_id=ONESIGNAL_APP_ID, rest_api_key=ONESIGNAL_REST_API_KEY) def send_notification(notification_body): try: onesignal_client.send_notification(notification_body) except OneSignalHTTPError as e: pass
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from flask_login import LoginManager from flask_socketio import SocketIO from onesignal_sdk.client import Client from WebPortal.Config.ConfigFlask import Config from WebPortal.Config.ConfigOneSignal import ConfigOneSignal db = SQLAlchemy() bcrypt = Bcrypt() loginMan = LoginManager() SocketIOClient = SocketIO() OneSignalClient = Client(app_id=ConfigOneSignal.App_ID, rest_api_key=ConfigOneSignal.Rest_API_Key) def create_WebPortal(config_class=Config): app = Flask(__name__) app.config.from_object(Config) db.init_app(app) bcrypt.init_app(app) loginMan.init_app(app) SocketIOClient.init_app(app) from WebPortal.Authentication.routes import Authentication from WebPortal.Usercontrol.routes import UserControl from WebPortal.MainPage.routes import MainPage from WebPortal.Notifications.routes import Notifications
def client(self): return Client(self.APP_ID, self.REST_API_KEY, self.USER_AUTH_TOKEN)