Exception in PayloadBuilder.shrinkBody()

58 views
Skip to first unread message

amitfrid

unread,
Jul 31, 2011, 2:04:48 PM7/31/11
to Java client for Apple Push Notification service (APNs), ra...@delegatecom.com
HI,
I am creating a push message using PayloadBuilder.
specifically I am building the alert part with a dictionary (map of
key-value pairs):

PushParams p = new PushParams(); //my private populated push params

PayloadBuilder payloadBuilder = APNS.newPayload();

String alertBody = p.getAps().getAlert();
payloadBuilder.alertBody(alertBody);

String action = p.getAps().getAction();
if(action == null) {
payloadBuilder.noActionButton();
} else {
payloadBuilder.actionKey(action);
}

when calling after the payload is populated (with too long body
value...) to
if (payloadBuilder.isTooLong()) {
payloadBuilder = payloadBuilder.shrinkBody();
}

I got an exception, that caused since the code assumes the alert value
is a String where it is actually a HashMap:

java.lang.ClassCastException: java.util.HashMap cannot be cast to
java.lang.String
at
com.notnoop.apns.PayloadBuilder.resizeAlertBody(PayloadBuilder.java:
283)
at com.notnoop.apns.PayloadBuilder.shrinkBody(PayloadBuilder.java:
345)
at com.notnoop.apns.PayloadBuilder.shrinkBody(PayloadBuilder.java:
328)
at
com.delegatecom.connector.NotnoopConnector.send(NotnoopConnector.java:
166)


Let me know if you need more info.
Thanks,
Amit

Mahmood Ali

unread,
Jul 31, 2011, 11:30:14 PM7/31/11
to java-apn...@googlegroups.com, ra...@delegatecom.com
Greetings,

Can you please submit a unit test. Your sample code here doesn't
reveal the problem.

Are you reusing the same instance of PayloadBuilder?

Thanks,
Mahmood

amitfrid

unread,
Aug 1, 2011, 5:04:49 AM8/1/11
to Java client for Apple Push Notification service (APNs), Ran Mochary
Hi,
I wrote a unit test which revealed the problem. It's actually not a
problem in shrinkBody ut rather in the data structure.
If I have a message who is too long, I am printing it to the log using
PayloadBuilder.toString(). This method actually calling
PayloadBuilder.build() which replace the String value in "alert" with
a HashMap, which then throw the exception when calling shrinkBody() -
since it's expecting a String again.
I think that calling toString() should not effect the data structure.
For example, you can't call it twice.

Thanks,
Amit

Amit Friedman

unread,
Aug 1, 2011, 5:17:42 AM8/1/11
to Java client for Apple Push Notification service (APNs), Ran Mochary
Attached is the failed unit test
PayloadBuilderFailedTest.java

Mahmood Ali

unread,
Aug 1, 2011, 12:19:07 PM8/1/11
to java-apn...@googlegroups.com, Ran Mochary
Greetings,

I just pushed a fix for this issue, changeset 8859c. It should be live in maven snapshot repository soon.

> I think that calling toString() should not effect the data structure.
> For example, you can't call it twice.

You can actually call it multiple times, so long as you don't mutate it after the first build ;-). The fix permits mutations between builds now.

Hope this works.

Regards,
Mahmood

amitfrid

unread,
Aug 1, 2011, 1:36:44 PM8/1/11
to Java client for Apple Push Notification service (APNs), Ran Mochary
Hi,
Thanks for the quick fix.
BTW, all fixed now goes to master(0.2.0 version), does this build
stable to use in production, or should I use the previous 0.1.6 for
now?

Thanks,
Amit
Reply all
Reply to author
Forward
0 new messages