Ejemplo n.º 1
0
    def send(self, subject, message, action):
        if not self.is_available():
            return

        data = {'title': subject, 'message': message, 'action': action}
        conn = gcm.GCM(self._apikey)
        conn.json_request(registration_ids=[self._clientkey], data=data)
Ejemplo n.º 2
0
 def setUp(self):
     # type: () -> None
     super(GCMTest, self).setUp()
     apn.gcm = gcm.GCM('fake key')
     self.gcm_tokens = [u'1111', u'2222']
     for token in self.gcm_tokens:
         PushDeviceToken.objects.create(kind=PushDeviceToken.GCM,
                                        token=apn.hex_to_b64(token),
                                        user=self.user_profile,
                                        ios_app_id=None)
Ejemplo n.º 3
0
def make_gcm_client() -> gcm.GCM:  # nocoverage
    # From GCM upstream's doc for migrating to FCM:
    #
    #   FCM supports HTTP and XMPP protocols that are virtually
    #   identical to the GCM server protocols, so you don't need to
    #   update your sending logic for the migration.
    #
    #   https://developers.google.com/cloud-messaging/android/android-migrate-fcm
    #
    # The one thing we're required to change on the server is the URL of
    # the endpoint.  So we get to keep using the GCM client library we've
    # been using (as long as we're happy with it) -- just monkey-patch in
    # that one change, because the library's API doesn't anticipate that
    # as a customization point.
    gcm.gcm.GCM_URL = 'https://fcm.googleapis.com/fcm/send'
    return gcm.GCM(settings.ANDROID_GCM_API_KEY)
Ejemplo n.º 4
0
def real_notify(user, event_id, extra_data):
    if not can_notify(user):
        logging.info("No android GCM tokens.")
        return

    # We don't pass debug=True, because gcm.py library keeps adding more loggers ad-infinitum.
    # Instead we call GCM.enable_logging() once at the top-level.
    g = gcm.GCM(keys.get('google_server_key'))
    tokens = user.device_tokens('android')

    data = {
        # Important data for clientside lookups
        'event_id': event_id,
    }
    data.update(extra_data)
    response = g.json_request(registration_ids=tokens, data=data)

    changed_tokens = False
    if 'errors' in response:
        for error, reg_ids in response['errors'].iteritems():
            if error in ('NotRegistered', 'InvalidRegistration'):
                for reg_id in reg_ids:
                    tokens.remove(reg_id)
                    changed_tokens = True
            else:
                logging.error("Error for user %s with event %s: %s",
                              user.fb_uid, event_id, error)

    if 'canonical' in response:
        for reg_id, canonical_id in response['canonical'].iteritems():
            tokens.remove(reg_id)
            tokens.append(canonical_id)
            changed_tokens = True

    if changed_tokens:
        user.put()

    logging.info("User %s (%s), event %s: sent notification!", user.fb_uid,
                 user.full_name, event_id)

    return 'success' in response
Ejemplo n.º 5
0
import os

import gcm
from flask import render_template
from gcm.gcm import GCMNotRegisteredException

from libapp import app
from libapp.config import pushconf

from .notifications import Notification

gs = gcm.GCM(pushconf.API_KEY, debug=pushconf.DEBUG)


class Push(Notification):
    def __init__(self, **kwargs):
        super(Push, self).__init__(**kwargs)

    def get_templates(self, template_name, **kwargs):
        """
        Get text templates for push
        """
        text = render_template("{template}.txt".format(template=template_name),
                               **kwargs)
        return text

    def get_message(self, **kwargs):
        """
        Get message object for push
        """
        gcm_keys = ["registration_id", "registration_ids", "topic", "retries"]
Ejemplo n.º 6
0
 def __init__(self, **data):
     super(GCM, self).__init__(**data)
     self.gcm = gcm.GCM(self.config['API_KEY'])
Ejemplo n.º 7
0
 def start(self):
     self.gcm = gcm.GCM(self.api_key)
     self.ret = {}
Ejemplo n.º 8
0
 def __init__(self):
     api_key = os.environ.get('GCM_SERVER_KEY')
     _logging = bool(os.environ.get('GCM_LOGGING'))
     self.client = gcm.GCM(api_key, debug=_logging)