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

1

7

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.

Genovera answered 21/7, 2014 at 21:49 Comment(2)
So, part of the problem is that their "getValidNotificationAttributes" method in AmazonSNSClientWrapper.java doesn't do a null check when it starts.Sharp
hi, i have certificate.pem and private key for sending apns notifications to my application. should these be verified before it can be used cos i am unable to send notifications. I followed the aws tutorial. can u help me.Broil
G
3

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.

Genovera answered 22/7, 2014 at 18:39 Comment(1)
Have u send notification to Ios app through the Programming Code?Wellknown

© 2022 - 2024 — McMap. All rights reserved.