Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Error on parsing GCM json structure #1663

Closed
clementcontet opened this issue Apr 3, 2017 · 8 comments
Closed

Error on parsing GCM json structure #1663

clementcontet opened this issue Apr 3, 2017 · 8 comments

Comments

@clementcontet
Copy link
Contributor

Expected Behaviour

When I send the following push notif to an Android:

{
    "message":
        {
            "title": "Title",
            "message" : "Message"
        }
}

I expect to get the correct title and message.

Actual Behaviour

Yet, for the body of the notif, I get {"title": "Title","message" : "Message"} instead of just Message

Analysis

Digging in the code, the issue is that the plugin accept "message" as the json node for payload content (apparently since pull #218).
See:

      // If normalizeKeythe key is "data" or "message" and the value is a json object extract
            // This is to support parse.com and other services. Issue #147 and pull #218
            if (key.equals(PARSE_COM_DATA) || key.equals(MESSAGE) || key.equals(messageKey)) {

But later, the key for the body of the notif is normalized to the same string "message".
See:

    private String normalizeKey(String key, String messageKey, String titleKey) {
        if (key.equals(BODY) || key.equals(ALERT) || key.equals(MP_MESSAGE) || key.equals(GCM_NOTIFICATION_BODY) || key.equals(TWILIO_BODY) || key.equals(messageKey)) {
            return MESSAGE;

And finally, in replaceKey method, the body of my notif with the key "message" get replaced with the whole payload (with the same key "message").

Is there a point to this last replaceKey method call? (as all previously written key/values have already been normalized/localized)

Reproduce Scenario

Steps to Reproduce

Send this payload to an Android

Platform and Version

Android 6.0.1

What device vendor

Nexus5

Cordova CLI version and cordova platform version

cordova --version                                    6.5.0
cordova platform version android                     6.1.2

Plugin version

cordova plugin version | grep phonegap-plugin-push   1.10.0

Sample Push Data Payload

{
    "message":
        {
            "title": "Title",
            "message" : "Message"
        }
}

Logs taken while reproducing problem

04-03 17:02:16.016 20441-27607/? D/PushPlugin_GCMIntentService: onMessage - from: 520313199651
04-03 17:02:16.017 20441-27607/? D/PushPlugin_GCMIntentService: normalize extras
04-03 17:02:16.017 20441-27607/? D/PushPlugin_GCMIntentService: key = google.sent_time
04-03 17:02:16.017 20441-27607/? D/PushPlugin_GCMIntentService: replace key google.sent_time with google.sent_time
04-03 17:02:16.018 20441-27607/? D/PushPlugin_GCMIntentService: key = EmbeddedMessageId
04-03 17:02:16.018 20441-27607/? D/PushPlugin_GCMIntentService: replace key EmbeddedMessageId with EmbeddedMessageId
04-03 17:02:16.018 20441-27607/? D/PushPlugin_GCMIntentService: key = google.message_id
04-03 17:02:16.018 20441-27607/? D/PushPlugin_GCMIntentService: replace key google.message_id with google.message_id
04-03 17:02:16.018 20441-27607/? D/PushPlugin_GCMIntentService: key = message
04-03 17:02:16.019 20441-27607/? D/PushPlugin_GCMIntentService: extracting nested message data from key = message
04-03 17:02:16.020 20441-27607/? D/PushPlugin_GCMIntentService: key = data/title
04-03 17:02:16.021 20441-27607/? D/PushPlugin_GCMIntentService: no locale found for key = title, error Value title of type java.lang.String cannot be converted to JSONObject
04-03 17:02:16.021 20441-27607/? D/PushPlugin_GCMIntentService: key = data/message
04-03 17:02:16.021 20441-27607/? D/PushPlugin_GCMIntentService: no locale found for key = message, error Value Message of type java.lang.String cannot be converted to JSONObject
04-03 17:02:16.021 20441-27607/? D/PushPlugin_GCMIntentService: replace key message with message
04-03 17:02:16.022 20441-27607/? D/PushPlugin_GCMIntentService: no locale found for key = message, error No value for locKey
04-03 17:02:16.022 20441-27607/? D/PushPlugin_GCMIntentService: background
04-03 17:02:16.023 20441-27607/? D/PushPlugin_GCMIntentService: message =[{"title":"title","message":"Message"}]
04-03 17:02:16.023 20441-27607/? D/PushPlugin_GCMIntentService: title =[title]
04-03 17:02:16.024 20441-27607/? D/PushPlugin_GCMIntentService: contentAvailable =[null]
04-03 17:02:16.024 20441-27607/? D/PushPlugin_GCMIntentService: forceStart =[null]
04-03 17:02:16.024 20441-27607/? D/PushPlugin_GCMIntentService: create notification
04-03 17:02:16.026 20441-27607/? E/PushPlugin_GCMIntentService: Number format exception - Error parsing notId: Invalid int: "null"
04-03 17:02:16.063 20441-27607/? D/PushPlugin_GCMIntentService: stored icon=push
04-03 17:02:16.065 20441-27607/? D/PushPlugin_GCMIntentService: stored iconColor=#ff7900
04-03 17:02:16.065 20441-27607/? D/PushPlugin_GCMIntentService: stored sound=true
04-03 17:02:16.066 20441-27607/? D/PushPlugin_GCMIntentService: stored vibrate=true
04-03 17:02:16.068 20441-27607/? D/PushPlugin_GCMIntentService: using icon from plugin options
04-03 17:02:16.079 20441-27607/? D/PushPlugin_GCMIntentService: create actions: with in-line
screenshot_20170403-170221

@macdonst
Copy link
Member

macdonst commented Apr 3, 2017

@clementcontet you are not sending the payload in the correct format. It should look like this:

{
    "data":
        {
            "title": "Title",
            "message" : "Message"
        }
}

@macdonst macdonst closed this as completed Apr 3, 2017
@clementcontet
Copy link
Contributor Author

@macdonst, I know the correct format, but as explained in pull #218, some push providers (as the one I use) don't let the choice!
Plus, there is partial support of this syntax (since pull #218):

      // If normalizeKeythe key is "data" or "message" and the value is a json object extract
            // This is to support parse.com and other services. Issue #147 and pull #218
            if (key.equals(PARSE_COM_DATA) || key.equals(MESSAGE) || key.equals(messageKey)) {

So it would be more coherent to remove completely support for this syntax, or to support it fully.

What do you think?

@macdonst
Copy link
Member

macdonst commented Apr 3, 2017

@clementcontet I'm not really keen on adding more exceptions but I would like to support you. The service you are using is not sending the payload in a typical format. What service is it?

@clementcontet
Copy link
Contributor Author

@macdonst, thanks, I'm using a private service my company is providing me. I don't know if it is based on a commercial product or in-house development. But it serves dozens of apps in our company and I fear that changing the payload format (even if possible) would lead to regression risks for others.

To complete the support for this syntax in your plugin, I can try to propose a pull request to fix this, without adding unnecessary case-specific code.

@macdonst
Copy link
Member

macdonst commented Apr 3, 2017

@clementcontet yeah, sending a PR is the best way forward on this issue.

@clementcontet
Copy link
Contributor Author

@macdonst, here it is: pull #1666

@mjsalinger
Copy link

+1 on this. The "message" format was actually the early format used by GCM and many push providers coded around this.

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants