Source

Project Mensa / projectmensa / menu / notifications.py

Full commit
from gcm import GCM

from rest_framework.renderers import JSONRenderer


def send_gcm_notification(title, menu):
    from .models import Device
    from .serializers import MenuSerializer

    gcm = GCM('AIzaSyBZHot2HdSSZiVBVPaAql1M-T93zFtZxeY')
    data = {
        'title': title,
        'menu': JSONRenderer().render(MenuSerializer(menu).data)
    }

    ids = [str(x) for x in Device.objects.all().values_list('key', flat=True)]

    gcm.json_request(
        registration_ids=ids, data=data,
        collapse_key='menu', delay_while_idle=True, time_to_live=3600
    )