def add_complaint(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is None: return JsonResponse([{'login_status': 0}], safe=False) title = request.POST['title'] description = request.POST['description'] # num_photos = request.POST['num_photos'] complaint = Complaint.objects.create(resident=user, township=user.township, title=title, description=description, timestamp=timezone.now(), resolved=False) beams_client = PushNotifications(instance_id=settings.BEAMS_INSTANCE_ID, secret_key=settings.BEAMS_SECRET_KEY) response = beams_client.publish_to_interests( interests=[str(user.township_id) + '-admins'], publish_body={ 'fcm': { 'notification': { 'title': 'New complaint!', 'body': user.first_name + ': ' + title, }, }, }, ) return JsonResponse([{'login_status': 1, 'request_status': 1}, {'complaint_id': complaint.id}], safe=False)
def test_generate_token_should_return_token(self): user_id = 'user-0001' pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) token_object = pn_client.generate_token(user_id) self.assertIsInstance(token_object, dict) token_string = token_object.get('token') self.assertIsInstance(token_string, six.string_types) self.assertTrue(len(token_string) > 0) decoded_token = jwt.decode( token_string, 'SECRET_KEY', algorithm='HS256', ) expected_issuer = 'https://INSTANCE_ID.pushnotifications.pusher.com' expected_subject = user_id self.assertEquals(decoded_token.get('iss'), expected_issuer) self.assertEquals(decoded_token.get('sub'), expected_subject) self.assertIsNotNone(decoded_token.get('exp')) self.assertTrue(decoded_token.get('exp') > time.time())
def push_not(): beams_client = PushNotifications( instance_id='661a95d4-7499-47b3-9e3f-5a03827fad36', secret_key='3300D2F6DE32046A00577E121354FAC46421E5922AFC2E6116D86E3E8BACD5C8', ) response = beams_client.publish_to_interests( interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Hello!' } }, 'fcm': { 'notification': { 'title': 'Hello', 'body': 'Hello, World!' } } } ) print(response['publishId'])
def push_notify(name,progress,work): from pusher_push_notifications import PushNotifications beams_client = PushNotifications( instance_id='068acacc-d54f-4853-ab93-a2ffbb3b4f89', secret_key='2965ADD942C08D08770BD5767E0D2949C0A467B1002DE65179D539F4302759DE', ) response = beams_client.publish_to_interests( interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Report created!' } }, 'fcm': { 'notification': { 'title': str(name), 'body': "Progress :" + str(progress) + "work :"+ str(work) } } } ) print(response['publishId'])
def vehicle_android_notify(title, vehicleNo, timedate): pn_client = PushNotifications( instance_id='8518a068-3e7f-4520-8b59-2799cb76b86c', secret_key= '9A1A920184592A9F281E7635D340E2C9C6D45C27156C7F1F742E6EF69662D67E', ) response = pn_client.publish( interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Report Created' } }, 'fcm': { 'notification': { 'title': str(title), 'body': 'Time: ' + str(timedate) + ', Black List Vehicle: ' + str(vehicleNo) + ' on main entrance' } } })
def push_notify(name, progress, work): from pusher_push_notifications import PushNotifications pn_client = PushNotifications( instance_id='be2dce08-9b23-4a81-a72b-fab39797530c', secret_key='0D2A5BABBF132520C4CF8FB9C165D12', ) response = pn_client.publish(interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Report Created' } }, 'fcm': { 'notification': { 'title': str(name), 'body': 'Progress: ' + str(progress) + " work: " + str(work) } } }) print(response['publishId'])
def push_notif(self, data): pn_client = PushNotifications( instance_id="5fb00808-3b8d-4533-9079-9fec45c7d781", secret_key='11027CDC0A5627F5F6EE83961C72D34', ) response = pn_client.publish( interests=[PUSHER_INTEREST], publish_body={ 'apns': { 'aps': { 'alert': 'Hello!', }, }, 'fcm': { 'notification': { 'title': 'ALERT MESSAGE', 'body': data['alarmname'], 'sound': 'default', }, }, }, ) print(response['publishId'])
def test_android_notify(title, vehicleNo, timedate): pn_client = PushNotifications( instance_id= 'dKEmXMZa2dg:APA91bGSPWTu1wgHL4IqYYpY3KN_X-HhylH1IQirN1AwIgqAx2jZZCIMuH_Yk9X0M5D6WtVMrWd-a0XqOCFY5Np8AEv6j87FeW7ObfDs5VpP3ABV2LeZnm51gzKbgZEca911Bc6XB53-', secret_key= 'dKEmXMZa2dg:APA91bGSPWTu1wgHL4IqYYpY3KN_X-HhylH1IQirN1AwIgqAx2jZZCIMuH_Yk9X0M5D6WtVMrWd-a0XqOCFY5Np8AEv6j87FeW7ObfDs5VpP3ABV2LeZnm51gzKbgZEca911Bc6XB53-', ) response = pn_client.publish( interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Report Created' } }, 'fcm': { 'notification': { 'title': str(title), 'body': 'Time: ' + str(timedate) + ', Black List Vehicle: ' + str(vehicleNo) + ' on main entrance' } } })
def push_notify(request): from pusher_push_notifications import PushNotifications print("Push notify called!") beams_client = PushNotifications( instance_id='422713b7-8870-499a-8534-5553787dc86c', secret_key= '6DACD3113B8FF98826AB73E91EB1BF4EADC216BBB8567B562A065F4BD1E71C60', ) print([request.POST.get("interest", "null")]) response = beams_client.publish_to_interests( interests=list([request.POST.get("interest", "null")]), # interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Notification form Dashboard!' } }, 'fcm': { 'notification': { 'title': str(request.POST.get("username", "not found")), 'body': str("Alert: " + request.POST.get("age", "null") + " years old.") } } }) return HttpResponse("Pass!")
def send_notification(title, message, topic="notifications"): beams_client = PushNotifications( instance_id=local_settings.instance_id, secret_key=local_settings.secret_key, ) try: response = beams_client.publish_to_interests(interests=[topic], publish_body={ 'apns': { 'aps': { 'alert': title } }, 'fcm': { 'notification': { 'title': title, 'body': message } } }) print(response['publishId']) except Exception as e: print(e)
def push_notify(request): from pusher_push_notifications import PushNotifications beams_client = PushNotifications( instance_id='422713b7-8870-499a-8534-5553787dc86c', secret_key='6DACD3113B8FF98826AB73E91EB1BF4EADC216BBB8567B562A065F4BD1E71C60', ) response = beams_client.publish_to_interests( interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Notification form Dashboard!' } }, 'fcm': { 'notification': { 'title': 'Notification from Dashboard!', 'body': 'Notification from Dashboard!' } } } ) return HttpResponse('Notification Sent')
def pushNotifyUser(name, progress, work): from pusher_push_notifications import PushNotifications beams_client = PushNotifications( instance_id='a3c258e1-27c2-4053-88a1-6d644fbf78d5', secret_key= 'E922B25E4167019F9CA1A482F9B1689A2E90E19FC4253BD184F545C76D15D78C', ) response = beams_client.publish_to_users(user_ids=['user-0001'], publish_body={ 'apns': { 'aps': { 'alert': 'Hello!' } }, 'fcm': { 'notification': { 'title': 'Hello', 'body': 'Hello, World!' } } }) print(response['publishId'])
def test_publish_to_interests_should_fail_if_too_many_interests_passed( self): pn_client = PushNotifications('INSTANCE_ID', 'SECRET_KEY') with requests_mock.Mocker() as http_mock: http_mock.register_uri( requests_mock.ANY, requests_mock.ANY, status_code=200, json={ 'publishId': '1234', }, ) with self.assertRaises(ValueError) as e: pn_client.publish_to_interests( interests=['interest-' + str(i) for i in range(0, 101)], publish_body={ 'apns': { 'aps': { 'alert': 'Hello World!', }, }, }, ) self.assertIn('Number of interests (101) exceeds maximum', str(e.exception))
def send_push(name, time): beams_client = PushNotifications( instance_id='97bc1b7f-aa2a-4760-af68-3052371c6dbd', secret_key= '17482EE2588EE046FBA7E20949EBB4CE00AA2325E6FCDDCD3E34202E0A79A5CB', ) response = beams_client.publish_to_interests( interests=['hello'], publish_body={ 'apns': { 'aps': { 'alert': 'Hello!' } }, 'fcm': { 'notification': { 'title': 'New access request', 'body': name + " has been requested to open at " + time } } }) print(response['publishId'])
def push_notify(nombre_lugar, nombre_producto, id_lugar): from pusher_push_notifications import PushNotifications pn_client = PushNotifications( instance_id='150ee5d4-aa83-42f8-9c65-fe6ab983f0ca', secret_key= 'FA39827AAB5E866F8084A0FD034F5CB59D987D566D35ED66BBEEA1564D561E93', ) message = nombre_lugar + ' ha agregado ' + nombre_producto + ' a sus productos' interest = str(id_lugar) response = pn_client.publish(interests=[interest], publish_body={ 'apns': { 'aps': { 'alert': 'Hello!' } }, 'fcm': { 'notification': { 'title': 'Nuevo Producto', 'body': message } } }) print(response['publishId'])
def test_delete_user_should_fail_if_user_id_not_a_string(self): pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) with self.assertRaises(TypeError) as e: pn_client.delete_user(False) self.assertIn('user_id must be a string', str(e.exception))
def test_delete_user_should_fail_if_user_id_too_long(self): pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) with self.assertRaises(ValueError) as e: pn_client.delete_user('A'*165) self.assertIn('longer than the maximum of 164 chars', str(e.exception))
def test_publish_to_interests_should_fail_if_body_not_dict(self): pn_client = PushNotifications('INSTANCE_ID', 'SECRET_KEY') with self.assertRaises(TypeError) as e: pn_client.publish_to_interests( interests=['donuts'], publish_body=False, ) self.assertIn('publish_body must be a dictionary', str(e.exception))
def test_generate_token_should_fail_if_user_id_not_a_string(self): user_id = False pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) with self.assertRaises(TypeError) as e: pn_client.generate_token(user_id) self.assertIn('user_id must be a string', str(e.exception))
def add_notice(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is None: return JsonResponse([{'login_status': 0}], safe=False) if user.type != 'admin': return JsonResponse([{'login_status': 1, 'authorization': 0}], safe=False) title = request.POST['title'] description = request.POST['description'] num_wings = request.POST['num_wings'] notice = Notice.objects.create(title=title, description=description, timestamp=timezone.now(), posted_by=user, township=user.township) beams_interests = [str(user.township_id) + '-admins'] for i in range(int(num_wings)): wing = Wing.objects.get(pk=request.GET['wing_' + str(i) + '_id']) beams_interests.append(str(user.township_id) + '-' + str(wing.id) + '-residents') notice.wings.add(wing) beams_client = PushNotifications(instance_id=settings.BEAMS_INSTANCE_ID, secret_key=settings.BEAMS_SECRET_KEY) response = beams_client.publish_to_interests( interests=beams_interests, publish_body={ 'fcm': { 'notification': { 'title': 'New notice!', 'body': title + ': ' + description, }, }, }, ) def generate_dict(notice): data_dict = {} data_dict['notice_id'] = notice.id data_dict['posted_by_first_name'] = notice.posted_by.first_name data_dict['posted_by_last_name'] = notice.posted_by.last_name data_dict['posted_by_designation'] = notice.posted_by.designation data_dict['timestamp'] = notice.timestamp data_dict['title'] = notice.title data_dict['description'] = notice.description # data_dict['wings'] = [{'wing_id': wing.id, 'wing_name': wing.name} for wing in wings] return data_dict wings = notice.wings.all() return JsonResponse( [{'login_status': 1, 'request_status': 1}, generate_dict(notice), [{'wing_id': wing.id} for wing in wings]], safe=False)
def __init__(self, helperHandler): self.helperHandler = helperHandler self.beams_client = PushNotifications( instance_id='7032df3e-e5a8-494e-9fc5-3b9f05a68e3c', secret_key= '8AC9B8AABB93DFE452B2EFC2714FCF923841B6740F97207F4512F240264FF493') self.userDao = UserDAO() self.containerDao = ContainerDAO() self.authDao = AuthDao() self.locationDao = LocationDao()
def __init__(self): ( pusher_beams_instance_id, pusher_beams_secret_key, ) = load_pusher_beams_config() self.pusher_beams_client = PushNotifications( instance_id=pusher_beams_instance_id, secret_key=pusher_beams_secret_key, )
def get_beams_token(request): username = request.headers['Username'] password = request.headers['Password'] user = authenticate(request, username=username, password=password) if user is None: return JsonResponse([{'login_status': 0}], safe=False) beams_client = PushNotifications(instance_id=settings.BEAMS_INSTANCE_ID, secret_key=settings.BEAMS_SECRET_KEY) beams_token = beams_client.generate_token(username) return JsonResponse(beams_token, safe=False)
def test_delete_user_should_not_error_on_not_json_success(self): pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) with requests_mock.Mocker() as http_mock: http_mock.register_uri( requests_mock.ANY, requests_mock.ANY, status_code=200, text='<notjson></notjson>', ) pn_client.delete_user('alice')
def test_publish_to_interests_should_fail_if_interest_too_long(self): pn_client = PushNotifications('INSTANCE_ID', 'SECRET_KEY') with self.assertRaises(ValueError) as e: pn_client.publish_to_interests( interests=['A' * 200], publish_body={ 'apns': { 'aps': { 'alert': 'Hello World!', }, }, }, ) self.assertIn('longer than the maximum of 164 chars', str(e.exception))
def test_publish_to_interests_should_fail_if_interest_not_a_string(self): pn_client = PushNotifications('INSTANCE_ID', 'SECRET_KEY') with self.assertRaises(TypeError) as e: pn_client.publish_to_interests( interests=[False], publish_body={ 'apns': { 'aps': { 'alert': 'Hello World!', }, }, }, ) self.assertIn('Interest False is not a string', str(e.exception))
def test_publish_to_interests_should_fail_if_no_interests_passed(self): pn_client = PushNotifications('INSTANCE_ID', 'SECRET_KEY') with self.assertRaises(ValueError) as e: pn_client.publish_to_interests( interests=[], publish_body={ 'apns': { 'aps': { 'alert': 'Hello World!', }, }, }, ) self.assertIn('must target at least one interest', str(e.exception))
def push_notify(title,vahicleNo,timedate): from pusher_push_notifications import PushNotifications pn_client = PushNotifications( instance_id='8518a068-3e7f-4520-8b59-2799cb76b86c', secret_key='9A1A920184592A9F281E7635D340E2C9C6D45C27156C7F1F742E6EF69662D67E', ) response = pn_client.publish( interests=['hello'], publish_body={'apns': {'aps': {'alert': 'Report Created'}}, 'fcm': {'notification': {'title': str(title), 'body': 'Black List Vehicle: '+str(vahicleNo) +'has entered' } }} ) print(response['publishId'])
def test_delete_user_should_raise_on_http_404_error(self): pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) with requests_mock.Mocker() as http_mock: http_mock.register_uri( requests_mock.ANY, requests_mock.ANY, status_code=404, json={'error': 'Instance not found', 'description': 'blah'}, ) with self.assertRaises(PusherMissingInstanceError) as e: pn_client.delete_user('user-0001') self.assertIn('Instance not found: blah', str(e.exception))
def test_delete_user_should_error_correctly_if_error_not_json(self): pn_client = PushNotifications( 'INSTANCE_ID', 'SECRET_KEY' ) with requests_mock.Mocker() as http_mock: http_mock.register_uri( requests_mock.ANY, requests_mock.ANY, status_code=500, text='<notjson></notjson>', ) with self.assertRaises(PusherServerError) as e: pn_client.delete_user('user-0001') self.assertIn('Unknown error: no description', str(e.exception))