o
    f
                     @   sd   d dl mZ d dlmZ d dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlZG dd deZdS )	    )render)JsonResponseN)credentials	messaging)View)FIREBASE_CREDENTIALS_PATHc                   @   s<   e Zd Zdd Ze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_adminZ_appsr   Certificater   Zinitialize_app)selfZcred r   9/Users/leeseokho/Desktop/Study/Cat_ch/server/fcm/views.py__init__
   s   
zpushNotificationView.__init__c                 C   s*   t jt j| |dt| d}t |S )Ntitlebody)notificationtokens)r   ZMulticastMessageNotificationlistvaluesZsend_multicast)r   contentsZ	fcm_token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Zsucess_countfailure_countr   	KEY_ERROR  status)
GETgetr   r   printr   r   formatsuccess_countr"   )r   requestr   r   r   r   r   r   r   r(   "   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   r#   r$   r%   )jsonloadsr   decoder   r   r)   r   r   r*   r+   r"   )r   r,   datar   r   r   r   post/   s   zpushNotificationView.postN)	__name__
__module____qualname__r   staticmethodr   r   r(   r1   r   r   r   r   r   	   s    
	
	r   )django.shortcutsr   django.httpr   r	   r   r   Zdjango.viewsr   Zcat_ch.settingsr   r-   r   r   r   r   r   <module>   s    