def send_push_notification(user_id, type, message, args='', use_sandbox=False): user = user_traces_db.load_user_info(user_id) apns = APNs(use_sandbox=use_sandbox, cert_file='apns_trackingadvisor.pem') token_hex = user['push_notification_id'] d = {'type': type} if type == "review": d['title'] = "You have personal information to review" d['message'] = "Do you want to review personal information about the places you visited?" payload = Payload(alert=message, sound="default", badge=1, custom=d) elif type == "timeline" and args != "": d['day'] = args d['title'] = "You have visits to review" d['message'] = "Do you want to review your visits yesterday?" payload = Payload(alert=message, sound="default", badge=1, custom=d) elif type == "web" and args != "": d['url'] = args d['title'] = "Show the final study survey" d['message'] = "Do you want to display the final study survey? This will help us get useful feedback from you." payload = Payload(alert=message, sound="default", badge=1, custom=d) elif type == "message" and args: d['title'] = "New message" d['message'] = "Do you want to read your new message?" d['msg'] = args['message'] d['timestamp'] = args['timestamp'] payload = Payload(alert={ 'title': "New message", 'body': args['message'] }, sound="default", badge=1, custom=d) if payload is not None: apns.gateway_server.send_notification(token_hex, payload)
def send_push_notification_update(user_id): user = user_traces_db.load_user_info(user_id) apns = APNs(use_sandbox=True, cert_file='apns_trackingadvisor.pem') token_hex = user['push_notification_id'] payload = Payload(content_available=True) apns.gateway_server.send_notification(token_hex, payload) print("Done")
def send_push_notification_test(user_id, message): user = user_traces_db.load_user_info(user_id) apns = APNs(use_sandbox=False, cert_file='apns_trackingadvisor.pem') token_hex = user['push_notification_id'] print(token_hex) payload = Payload(alert=message, sound="default", badge=1) apns.gateway_server.send_notification(token_hex, payload) for (token_hex, fail_time) in apns.feedback_server.items(): print(token_hex, fail_time) print("Done")
def send_enhanced_push_notification(user_id, message): import random def response_listener(error_response): print("client get error-response: " + str(error_response)) user = user_traces_db.load_user_info(user_id) token_hex = user['push_notification_id'] apns_enhanced = APNs(use_sandbox=False, cert_file='apns_trackingadvisor.pem', enhanced=True) payload = Payload(alert=message, sound="default", badge=1) identifier = random.getrandbits(32) apns_enhanced.gateway_server.send_notification(token_hex, payload, identifier=identifier) apns_enhanced.gateway_server.register_response_listener(response_listener)
def send_push_notification_review_challenge(user_id, day, message): user = user_traces_db.load_user_info(user_id) review_challenge = user_traces_db.load_user_review_challenge(user_id, day) review_challenge_days = list(set([rc['day'] for rc in review_challenge])) print(review_challenge_days) apns = APNs(use_sandbox=True, cert_file='apns-cert.pem', key_file='apns-key-noenc.pem') token_hex = user['push_notification_id'] payload = Payload(alert=message, sound="default", badge=1, category="REVIEW_CHALLENGE", custom={'challenges': review_challenge_days}) apns.gateway_server.send_notification(token_hex, payload) print("Done")
def send_survey_notification_to_user(user_id, url, use_sandbox=False): user = user_traces_db.load_user_info(user_id) apns = APNs(use_sandbox=use_sandbox, cert_file='apns_trackingadvisor.pem') token_hex = user['push_notification_id'] title = "Hey there 👋" body = "Thank you for participating in the study! We have a survey for you to fill ... Tap to open it!" alert = {"title": title, "body": body} d = { "type": "web", "url": url + "?user_id=%s" % user_id, "title": title, "message": body } payload = Payload(alert=alert, sound="default", badge=1, custom=d) apns.gateway_server.send_notification(token_hex, payload)