Last active
December 27, 2015 10:49
-
-
Save cirocosta/7314112 to your computer and use it in GitHub Desktop.
GCM Little hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from uuid import uuid1 | |
import json | |
import urllib2 | |
class Mensageiro(): | |
def __init__(self,conteudo,devices,delay_while_idle,collapse_key): | |
self.conteudo = conteudo | |
self.big_message = False | |
self.devices = devices | |
self.number_of_sends = 0 | |
def get_number_of_sends(self): | |
number_of_devices = len(self.devices) | |
if number_of_devices > 999: | |
self.number_of_sends = number_of_devices/1000 | |
elif number_of_devices < 1: | |
return 0 | |
def check_big_message(self): | |
tamanho = len(unicode(self.conteudo,'utf-8')) | |
if tamanho > 4000: | |
self.big_message = True | |
else: | |
self.big_message = False | |
def create_big_message(self): | |
if self.big_message: | |
big_message = BigMessage.objects.create(key_bigmessage=genKey(),\ | |
mensagem=self.conteudo) | |
return big_message.key_bigmessage | |
else: | |
return False | |
def send_message(self): | |
responses = list() | |
for i in range(self.number_of_sends): | |
data = { | |
"registration_ids": self.device_regids[i*1000:(i+1)*1000], | |
"data": self.conteudo | |
} | |
if self.collapse_key: | |
data['collapse_key'] = collapse_key | |
if self.delay_while_idle: | |
data['delay_while_idle'] = True | |
data = json.dumps(data) | |
req = urllib2.Request(url,data,headers) | |
f = urllib2.urlopen(req) | |
response = f.read() | |
responses.append(response) | |
return responses | |
def genKey(): | |
return uuid1().hex | |
class BigMessage(models.Model): | |
key_bigmessagegcm = models.CharField(max_length=200) | |
mensagem = models.TextField() | |
data_criacao = models.DateField(auto_now_add=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment