def send_email(self, user_external_id: str, sender_email: str, sender_name: str, to_email: str, subject: str, plain_text_content: str, html_content: str) -> Tuple[EmailResult, str]: ''' Sends. Just prints and create BaseEmail entity in DB ''' params = { 'subject': subject, 'from_email': sender_email, 'recipient_list': [to_email], } if plain_text_content: params['message'] = plain_text_content if html_content: params['html_message'] = html_content log_debug_info(__name__, "send_email", params) send_mail(**params, fail_silently=False) external_id = str(uuid.uuid4()) BaseEmail.create( { 'sender_email': sender_email, 'sender_name': sender_name, 'subject': subject, 'plain_text_content': plain_text_content, 'html_content': html_content }, to_email, self.get_type(), EmailResult.SENT, external_id, user_external_id) return EmailResult.SENT, external_id
def fcm_send_data_message( registration_id: str, data_message: dict, api_key: str = None, ): log_debug_info("fcm", "fcm_send_data_message", ">>") ''' Use data messages when you want to process the messages on your client app. FCM(iOS, Android, and web) sees this is as - { "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data":{ "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" } } } ''' if api_key is None: api_key = settings.NOTIF_MOBILE_FCM_SERVER_KEY push_service = gpush_service(api_key=api_key) result = push_service.single_device_data_message( registration_id=registration_id, data_message=data_message) log_debug_info("fcm", "fcm_send_data_message", "<<") return result
def unsubscribe_registration_ids_to_topic( topic: str, registration_ids: List[str], api_key: str = None) -> bool: # pragma: no cover log_debug_info("fcm", "unsubscribe_registration_ids_to_topic", ">>") if api_key is None: api_key = settings.NOTIF_MOBILE_FCM_SERVER_KEY push_service = gpush_service(api_key=api_key) unsubscribed = push_service.unsubscribe_registration_ids_to_topic( registration_ids, topic) log_debug_info("fcm", "unsubscribe_registration_ids_to_topic", "<<") return unsubscribed
def get_clean_registration_ids(registration_ids: List[str], api_key: str = None): # pragma: no cover ''' Can be used to periodically clean the stored IDs. ''' log_debug_info("fcm", "get_clean_registration_ids", ">>") if api_key is None: api_key = settings.NOTIF_MOBILE_FCM_SERVER_KEY push_service = gpush_service(api_key=api_key) clean_ids = push_service.clean_registration_ids(registration_ids) log_debug_info("fcm", "get_clean_registration_ids", "<<") return clean_ids
def fcm_send_display_message(registration_id: str, message_title: str, message_body: str, data_message: dict = None, api_key: str = None): ''' Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app. FCM can send a notification message including an optional data payload. In such cases, FCM handles displaying the notification payload, and the client app handles the data payload. FCM sees this as - { "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification":{ "title":"Portugal vs. Denmark", "body":"great match!" }, "data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } } } ''' log_debug_info("fcm", "fcm_send_display_message", ">>") if api_key is None: api_key = settings.NOTIF_MOBILE_FCM_SERVER_KEY push_service = gpush_service(api_key=api_key) kwargs: Dict[str, Any] = {registration_id: registration_id} if message_title: kwargs['message_title'] = message_title if message_body: kwargs['message_body'] = message_body if data_message: kwargs['data_message'] = data_message result = push_service.notify_single_device(**kwargs) log_debug_info("fcm", "fcm_send_display_message", "<<") return result
def fcm_send_display_message_to_topic(message_body: str, topic_name: str = None, condition: str = None, api_key: str = None): # pragma: no cover ''' Send display message to either named topic or via condition Followig is an example of topic condition topic_condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)" ''' log_debug_info("fcm", "fcm_send_display_message_to_topic", ">>") if api_key is None: api_key = settings.NOTIF_MOBILE_FCM_SERVER_KEY push_service = gpush_service(api_key=api_key) kwargs: Dict[str, Any] = {"message_body": message_body} if topic_name: kwargs["topic_name"] = topic_name else: kwargs["condition"] = condition result = push_service.notify_topic_subscribers(**kwargs) log_debug_info("fcm", "fcm_send_display_message_to_topic", "<<") return result