Skip to content Skip to sidebar Skip to footer

Gcm Invalid Json Missing Payload

I am trying to send messages through Google Cloud Messaging, using Python sleekXMPP. I tried to follow the sample in the GCM docs. However, I am getting an 'InvalidJson : MissingPa

Solution 1:

It turns out SleekXMPP was automatically enclosing my message in <body /> tags, which is not the expected message format by the GCM server. I ended up solving the problem by defining my own stanzas, like this:

class Gcm(ElementBase):
    namespace = 'google:mobile:data'
    name = 'gcm'
    plugin_attrib = 'gcm'
    interfaces = set('gcm')
    sub_interfaces = interfaces

class GcmMessage(ElementBase):
    namespace = ''
    name = 'message'
    interfaces = set('gcm')
    sub_interfaces = interfaces
    subitem = (Gcm,)

register_stanza_plugin(GcmMessage, Gcm)

and then by sending the message like this:

def send_gcm_message(self, message):
    msg = GcmMessage()
    msg['gcm'].xml.text = xml.sax.saxutils.escape(json.dumps(message, ensure_ascii=False))
    self.send(msg)

Solution 2:

The error you are receiving for Missing Payload is because you are not passing any parameter as Key and value format to be recognized as payload.

First you have to define a function where you are going to pass the payload and then covert it back to the plain text after the GCm server responds back.

Here is an example:

defplaintext_request(self, registration_id, data=None, collapse_key=None,
                          delay_while_idle=False, time_to_live=None, retries=5, dry_run=False):
ifnot registration_id:
        raise GCMMissingRegistrationException("Missing registration_id")

    payload = self.construct_payload(
        registration_id, data, collapse_key,
        delay_while_idle, time_to_live, False, dry_run
    )

You can have the exponential Back-Off mechanism if you need to have in your code that would ping the server after some duration.

For the detail code implementation, please read the following document.

Post a Comment for "Gcm Invalid Json Missing Payload"