Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom properties don't allow non-string values #974

Closed
jchambers opened this issue Jul 21, 2022 Discussed in #973 · 1 comment · Fixed by #976
Closed

Custom properties don't allow non-string values #974

jchambers opened this issue Jul 21, 2022 Discussed in #973 · 1 comment · Fixed by #976
Milestone

Comments

@jchambers
Copy link
Owner

jchambers commented Jul 21, 2022

Discussed in #973

Originally posted by Pro100proff July 21, 2022
I have encountered with problem when I wanted to add custom property as my own object. I expected json object like this:

{
       "aps": {
        "alert": {
            "body": "body text",
            "title": "title text"
        },
        "sound": "default",
        "badge": 1
    },
    "data": {
        "deeplink": "rupodari://wallet"
    }
}

In reality, I got:

{
       "aps": {
        "alert": {
            "body": "body text",
            "title": "title text"
        },
        "sound": "default",
        "badge": 1
    },
    "data": "Data(deeplink=\"rupodari:\/\/wallet\")"
}

I checked the implementation and realised that it used toString method for the serialization custom properties.
Is there work around to solve it?


…but the APNs docs say:

In addition to the Apple-defined keys, you may add custom keys to your payload to deliver small amounts of data to your app, notification service app extension, or notification content app extension. Your custom keys must have values with primitive types, such as dictionary, array, string, number, or Boolean. Custom keys are available in the userInfo dictionary of the UNNotificationContent object delivered to your app.

…so Pushy's payload builder should probably allow custom properties with non-string values.

@jchambers jchambers added the bug label Jul 21, 2022
@jchambers
Copy link
Owner Author

jchambers commented Jul 31, 2022

@Pro100proff I've looked into this a little more and I think I misunderstood your original report. To be more precisely state what I think is happening here, I understand that:

  • You are using a SimpleApnsPayloadBuilder
  • You are trying to set a custom property, and that value of that property is not a Java primitive, array of primitives, a Collection, or a Map

If I'm understanding you correctly, then this behavior is expected and is, in fact, documented:

/**
* <p>Adds a custom property to the payload. According to Apple's documentation:</p>
*
* <blockquote>Providers can specify custom payload values outside the Apple-reserved {@code aps} namespace. Custom
* values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
* You should not include customer information (or any sensitive data) as custom payload data. Instead, use it for
* such purposes as setting context (for the user interface) or internal metrics. For example, a custom payload
* value might be a conversation identifier for use by an instant-message client application or a timestamp
* identifying when the provider sent the notification. Any action associated with an alert message should not be
* destructive—for example, it should not delete data on the device.</blockquote>
*
* <p>The value for the property is serialized to JSON as described in {@link JsonSerializer}.</p>
*
* @param key the key of the custom property in the payload object
* @param value the value of the custom property
*
* @return a reference to this payload builder
*
* @throws IllegalArgumentException if the key equals to "aps"
*/
public ApnsPayloadBuilder addCustomProperty(final String key, final Object value) {
if (APS_KEY.equals(key)) {
throw new IllegalArgumentException("Custom property key must not be aps");
}
this.customProperties.put(key, value);
return this;
}

If you want to change how custom properties are serialized, you'll need to use a different ApnsPayloadBuilder implementation. Pushy offers two as separate modules: GsonApnsPayloadBuilder and JacksonApnsPayloadBuilder.

That said, I think there's room to make the documentation clearer in this case and will open a pull request to do exactly that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant