コード例 #1
0
	def test_bulk_push_payload(self):
		with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_JSON_RESPONSE) as p:
			gcm_send_bulk_message(["abc", "123"], {"message": "Hello world"})
			p.assert_called_once_with(
				b'{"data":{"message":"Hello world"},"registration_ids":["abc","123"]}',
				"application/json",
				None)
コード例 #2
0
 def test_bulk_push_payload(self):
     with mock.patch("push_notifications.gcm._gcm_send",
                     return_value=GCM_JSON_RESPONSE) as p:
         gcm_send_bulk_message(["abc", "123"], {"message": "Hello world"})
         p.assert_called_once_with(
             b'{"data":{"message":"Hello world"},"registration_ids":["abc","123"]}',
             "application/json")
コード例 #3
0
 def send_gcm(self, data, **kwargs):
     from push_notifications.gcm import gcm_send_bulk_message
     if not self.has_gcm:
         return
     gcm_send_bulk_message(
         registration_ids=[self.gcm_device.registration_id],
         data=data,
         **kwargs)
コード例 #4
0
	def test_send_message_with_no_reg_ids(self):
		self._create_devices(["abc", "abc1"])

		with mock.patch("push_notifications.gcm._gcm_send_plain", return_value="") as p:
			GCMDevice.objects.filter(registration_id="xyz").send_message("Hello World")
			p.assert_not_called()

		with mock.patch("push_notifications.gcm._gcm_send_json", return_value="") as p:
			reg_ids = [obj.registration_id for obj in GCMDevice.objects.all()]
			gcm_send_bulk_message(reg_ids, {"message": "Hello World"})
			p.assert_called_once_with([u"abc", u"abc1"], {"message": "Hello World"})
コード例 #5
0
    def test_send_message_with_no_reg_ids(self):
        device_list = ['abc', 'abc1']
        self.create_devices(device_list)

        with mock.patch("push_notifications.gcm._gcm_send_plain", return_value='') as p:
            GCMDevice.objects.filter(registration_id='xyz').send_message('Hello World')
            p.assert_not_called()

        with mock.patch("push_notifications.gcm._gcm_send_json", return_value='') as p:
            reg_ids = [obj.registration_id for obj in GCMDevice.objects.all()]
            gcm_send_bulk_message(reg_ids, {"message": "Hello World"})
            p.assert_called_once_with([u"abc", u"abc1"], {"message": "Hello World"})
コード例 #6
0
def push_to_android_devices(title_en,
                            title_pl,
                            message_en,
                            message_pl,
                            notification_type=NOTIFICATION_TYPE_BASIC):
    registration_ids = list(
        GCMDevice.objects.filter(active=True).values_list("registration_id",
                                                          flat=True))
    collapse_key = ''
    if notification_type is NOTIFICATION_TYPE_BASIC:
        collapse_key = 'basic'
    elif notification_type is NOTIFICATION_TYPE_ALERT:
        collapse_key = 'alert'
    elif notification_type is NOTIFICATION_TYPE_UPDATE:
        collapse_key = 'update'
    data = gcm_send_bulk_message(registration_ids=registration_ids,
                                 data={
                                     'title_en': title_en,
                                     'title_pl': title_pl,
                                     'message_en': message_en,
                                     'message_pl': message_pl,
                                     'type': notification_type
                                 },
                                 collapse_key=collapse_key)
    GCMDevice.deactivate_unused(registration_ids, data)
コード例 #7
0
    def test_send_message_with_no_reg_ids(self):
        device_list = ['abc', 'abc1']
        self.create_devices(device_list)

        with mock.patch("push_notifications.gcm._gcm_send_plain",
                        return_value='') as p:
            GCMDevice.objects.filter(
                registration_id='xyz').send_message('Hello World')
            p.assert_not_called()

        with mock.patch("push_notifications.gcm._gcm_send_json",
                        return_value='') as p:
            reg_ids = [obj.registration_id for obj in GCMDevice.objects.all()]
            gcm_send_bulk_message(reg_ids, {"message": "Hello World"})
            p.assert_called_once_with([u"abc", u"abc1"],
                                      {"message": "Hello World"})
コード例 #8
0
    def create(self, validated_data):
        apns = validated_data.get('apns')
        gcm = validated_data.get('gcm')
        payload = validated_data.get('payload', {})
        aps = payload.pop('aps', {})
        alert = aps.pop('alert', {})

        if apns:
            apns_send_bulk_message(registration_ids=[apns],
                                   alert=alert,
                                   extra=payload,
                                   **aps)
        if gcm:
            gcm_send_bulk_message(registration_ids=[gcm], data=payload)

        return True
コード例 #9
0
def gcm_send_bulk_message(registration_ids, data, collapse_key=None, delay_while_idle=False):
	"""
	Sends a GCM notification to one or more registration_ids. The registration_ids
	needs to be a list.
	This will send the notification as json data.
	"""

	# GCM only allows up to 1000 reg ids per bulk message
	# https://developer.android.com/google/gcm/gcm.html#request
	max_recipients = SETTINGS.get("GCM_MAX_RECIPIENTS")
	if len(registration_ids) > max_recipients:
		ret = []
		for chunk in _chunks(registration_ids, max_recipients):
			ret.append(gcm_send_bulk_message(chunk, data, collapse_key, delay_while_idle))
		return "\n".join(ret)

	values = {"registration_ids": registration_ids}

	if data is not None:
		values["data"] = data

	if collapse_key:
		values["collapse_key"] = collapse_key,

	if delay_while_idle:
		values["delay_while_idle"] = delay_while_idle

	data = json.dumps(values, separators=(",", ":")).encode("utf-8")
	return _gcm_send(data, "application/json")
コード例 #10
0
	def send_message(self, message, **kwargs):
		if self:
			from .gcm import gcm_send_bulk_message
			data = kwargs.pop("extra", {})
			if message is not None:
				data["message"] = message
			return gcm_send_bulk_message(
				registration_ids=list(self.values_list("registration_id", flat=True)),
				data=data)
コード例 #11
0
def _send_gcm(registration_ids, message, **kwargs):
    '''
    Send a message to one or more GCM recipients

    :param registration_ids: a single or iterable collection of registraion ids (GCM tokens)
    :param message: the payload to send. This is sent as the value of the 'message' GCM key,
    itself within the 'extra' key.
    :param kwargs: additional GCM arguments. Currently inserted directly into the payload
    '''

    data = kwargs.pop("extra", {})

    if message is not None:
        data["message"] = message

    if isinstance(registration_ids, collections.Iterable):
        gcm_send_bulk_message(registration_ids, data, **kwargs)
    else:
        gcm_send_message(registration_ids, data, **kwargs)
コード例 #12
0
def send_gcm(registration_ids, message, **kwargs):
    '''
    Send a message to one or more GCM recipients

    :param registration_ids: a single or iterable collection of registraion ids (GCM tokens)
    :param message: the payload to send. This is sent as the value of the 'message' GCM key,
    itself within the 'extra' key.
    :param kwargs: additional GCM arguments. Currently inserted directly into the payload
    '''

    data = kwargs.pop("extra", {})

    if message is not None:
        data["message"] = message

    if isinstance(registration_ids, collections.Iterable):
        gcm_send_bulk_message(registration_ids, data, **kwargs)
    else:
        gcm_send_message(registration_ids, data, **kwargs)
コード例 #13
0
	def test_bulk_push_payload(self):
		with mock.patch("push_notifications.gcm._gcm_send") as p:
			gcm_send_bulk_message(["abc", "123"], {'message': 'Hello world'})
			p.assert_called_once_with(
				'{"data":{"message":"Hello world"},"registration_ids":["abc","123"]}',
				'application/json')