/** * Subscribes to a given Amazon SNS topic. * * @param topic topic to subscribe to */ public void subscribeToTopic(final SnsTopic topic) { final SubscribeRequest request = new SubscribeRequest(); request.setEndpoint(endpointArn); request.setTopicArn(topic.getTopicArn()); request.setProtocol(SNS_PROTOCOL_APPLICATION); final SubscribeResult result = sns.subscribe(request); // update topic and save subscription in shared preferences final String subscriptionArn = result.getSubscriptionArn(); topic.setSubscriptionArn(subscriptionArn); sharedPreferences.edit().putString(topic.getTopicArn(), subscriptionArn).commit(); }
/** * Unsubscribes from a given Amazon SNS topic. * * @param topic topic to unsubscribe from */ public void unsubscribeFromTopic(final SnsTopic topic) { // Rely on the status stored locally even though it's likely that the device is // subscribed to a topic, but the subscription arn is lost, say due to clearing app data. if (!topic.isSubscribed()) { return; } final UnsubscribeRequest request = new UnsubscribeRequest(); request.setSubscriptionArn(topic.getSubscriptionArn()); sns.unsubscribe(request); // update topic and save subscription in shared preferences topic.setSubscriptionArn(""); sharedPreferences.edit().putString(topic.getTopicArn(), "").commit(); }