Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in the Amazon SNS Apple Push Notification Tutorial (Null Pointer Exception)

My goal end goal is to send push notifications to an iOS app via SNS. I am stepping through this tutorial: http://docs.aws.amazon.com/sns/latest/dg/mobile-push-apns.html.

I have added in my AWS credentials, and added in the corresponding apns credentials for my development key, certificate, private key, and a current push token for my app. When I run the tutorial I get:

Exception in thread "main" java.lang.NullPointerException
at com.amazonaws.sns.samples.tools.AmazonSNSClientWrapper.getValidNotificationAttributes(AmazonSNSClientWrapper.java:162)
at com.amazonaws.sns.samples.tools.AmazonSNSClientWrapper.publish(AmazonSNSClientWrapper.java:80)
at com.amazonaws.sns.samples.tools.AmazonSNSClientWrapper.demoNotification(AmazonSNSClientWrapper.java:131)
at com.amazonaws.sns.samples.mobilepush.SNSMobilePush.demoAppleSandboxAppNotification(SNSMobilePush.java:438)
at com.amazonaws.sns.samples.mobilepush.SNSMobilePush.main(SNSMobilePush.java:68)

At the top of SNSMobilePush.java there is a Map called attributesMap. It originally has values set to null for keys Platform.APNS and Platform.APNS_SANDBOX. These values never get changed anywhere during the code and are responsible for causing the null pointer exception. The tutorial does not indicate to change these values.

I did not do anything above or beyond the tutorial instructions.

I know that my credentials are correct as I did send a message to my iOS app using these same credentials via Amazon Management Console.

Can anyone indicated

  • if the tutorial is incomplete
  • what the values associated with Platform.APNS_SANDBOX should be in order to get this working
  • any hint to help me trouble shoot this

update I added in a null check to getValidNotificationAttributes() and now I am able to send push notifications using sns and apns using this tutorial.

like image 822
miss.serena Avatar asked Jul 21 '14 21:07

miss.serena


1 Answers

I was able to get the tutorial working by adding a null check in to the getValidNotificationAttributes() in the AmazonSNSClientWrapper class. I am convinced that this is a flaw in the code that is exposed when using Platform APNS_SANDBOX and APNS (and probably also ADM and GCM).

public static Map<String, MessageAttributeValue> getValidNotificationAttributes(
Map<String, MessageAttributeValue> notificationAttributes) {
if (notificationAttributes != null) {

Map<String, MessageAttributeValue> validAttributes = new HashMap<String, MessageAttributeValue>();
for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes.entrySet()) {
if (!StringUtils.isBlank(entry.getValue().getStringValue())) {
validAttributes.put(entry.getKey(), entry.getValue());
}
}
return validAttributes;
} else {
return new HashMap<String, MessageAttributeValue>();
}
}

I hope this helps anyone else who is working through this online tutorial.

like image 119
miss.serena Avatar answered Nov 08 '22 04:11

miss.serena