o
    6e                     @   sp   d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dl	Z	d dl	m
Z
mZ d dlmZ G dd	 d	eZdS )
    )render)View)JsonResponseN)FirebaseManager)credentials	messaging)FIREBASE_CREDENTIALS_PATHc                   @   s>   e Zd Zdd ZedddZedd Zdd	 Zd
d ZdS )pushNotificationViewc                 C   s"   t jstt}t | d S d S N)firebase_admin_appsr   Certificater   initialize_app)selfcred r   +/home/api_server/kakao_chatbot/fcm/views.py__init__   s   
zpushNotificationView.__init__Nc                 C   sV   t  }|d u r| }n|}tjtj| |dt| d}tt|  t|S )Ntitlebody)notificationtokens)	r   getTokenr   MulticastMessageNotificationlistvaluesprintsend_multicast)r   contents	fcm_tokenfbfcm_token_dictmessager   r   r   multicastMessage   s   

z%pushNotificationView.multicastMessagec                 C   s"   t jt j| |d|d}t |S )Nr   )r   token)r   Messager   send)r   r    r&   r$   r   r   r   unicastMessage   s   
z#pushNotificationView.unicastMessagec                 C   s   |j d|j d|j d}}}|d ur)t|||}t| tddiS |d urH|d urHt||}td|j t|j|j	dS tdd	id
dS )Nr   r    r&   responsegood:#{0} messages were sent successfullysucess_countfailure_countr$   	KEY_ERROR  status)
GETgetr	   r)   r   r   r%   formatsuccess_countr/   )r   requestr   r    r&   r*   r   r   r   r5   '   s   (zpushNotificationView.getc                 C   s   t |jd}d|v r$t|d |d |d }t| tddiS d|v rGd|v rGt|d |d }td	|j
 t|j
|jdS td	d
iddS )Nzutf-8r&   r   r    r*   r+   r,   r-   r$   r0   r1   r2   )jsonloadsr   decoder	   r)   r   r   r%   r6   r7   r/   )r   r8   datar*   r   r   r   post4   s   zpushNotificationView.postr
   )	__name__
__module____qualname__r   staticmethodr%   r)   r5   r=   r   r   r   r   r	   
   s    
	r	   )django.shortcutsr   django.viewsr   django.httpr   r9   userInfo.viewsr   r   r   r   kakao_chatbot.settingsr   r	   r   r   r   r   <module>   s    