import json
import requests
from Suchary.local_settings import GCM_API_KEY
from api.models import Device

URL = 'https://android.googleapis.com/gcm/send'
HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}


def get_reg_ids():
    reg_ids = [device.registration_id for device in Device.objects.filter(active=True)]
    return reg_ids


def send(data, collapse_key=None):
    reg_ids = get_reg_ids()
    payload = {'registration_ids': reg_ids, 'data': data}
    if collapse_key is not None:
        payload.update({'collapse_key': collapse_key})
    r = requests.post(URL, data=json.dumps(payload), headers=HEADER)


def edit_joke(key):
    data = {
        'type': 'edit',
        'key': key
    }
    send(data)


def new_jokes():
    data = {
        'type': 'new'
    }
    send(data, 'new')


def delete_joke(key):
    data = {
        'type': 'delete',
        'key': key
    }
    send(data)


def send_message(title, body):
    data = {
        'type': 'message',
        'title': title,
        'text': body
    }
    send(data)
import json

import requests

from Suchary.local_settings import GCM_API_KEY
from api.models import Device


URL = 'https://android.googleapis.com/gcm/send'
HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}


def get_reg_ids(alias):
    devices = Device.objects.filter(active=True)
    if alias:
        devices = devices.filter(alias=alias)
    reg_ids = [device.registration_id for device in devices]
    return reg_ids


def send(data, collapse_key=None, to=None):
    reg_ids = get_reg_ids(to)
    payload = {'registration_ids': reg_ids, 'data': data}
    if collapse_key is not None:
        payload.update({'collapse_key': collapse_key})
    r = requests.post(URL, data=json.dumps(payload), headers=HEADER)


def edit_joke(key):
    data = {
        'type': 'edit',
        'key': key
    }
    send(data)


def new_jokes():
    data = {
        'type': 'new'
    }
    send(data, 'new')


def delete_joke(key):
    data = {
        'type': 'delete',
        'key': key
    }
    send(data)


def send_message(title, body, alias=None):
    data = {
        'type': 'message',
        'title': title,
        'text': body
    }
    send(data, to=alias)
Send message only to one device if its alias was specified.