SNS aws工作不正常


大家好,我写了一个从facebook上抓取照片的应用程序。我成功地做到了。现在我用java编写了一个使用SNS的通知服务。基本上是为首次登录我的应用程序的用户以及从存储库中删除照片的用户发送订阅。我的第一个问题是,当我从脸书下载照片和用户信息时,我想检查它是否是新用户。如果一个新用户发送了订阅,如果没有(基本上用户存在于mongoDb中,不要发送订阅电子邮件),但我的代码不断向每个用户发送电子邮件。最后,当用户删除照片时,他们会收到通知,但当我测试时,我没有收到电子邮件。下面是我的代码,有人能告诉我我做错了什么吗。
public class EmailNotifications {
private static final String accessKey = "****************";
private static final String secretAccess ="***********************";
//Notification when user gets info from facebook app for first time. 
public static void SignUP(String email, String Topic){
AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(accessKey, secretAccess));
snsClient.setRegion(Region.getRegion(Regions.US_WEST_1));
//create a Topic
CreateTopicRequest createTopicRequest = new CreateTopicRequest().withName(Topic);
CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest);
//subscribes to a topic
SubscribeRequest subscribeRequest = new SubscribeRequest().withTopicArn(createTopicResult.getTopicArn())
.withProtocol("email").withEndpoint(email);
snsClient.subscribe(subscribeRequest);
}
//Notification when photo is deleted
public static void deletePic(String email, String topic, String message){
AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(accessKey,secretAccess));
snsClient.setRegion(Region.getRegion(Regions.US_WEST_1));
CreateTopicRequest create = new CreateTopicRequest(topic);
CreateTopicResult result = snsClient.createTopic(create);
System.out.println(result);
//String msg = "My text published to SNS topic with email endpoint";
PublishRequest publishRequest = new PublishRequest(result.getTopicArn(), message);
publishRequest.setSubject("Deleted Pic");
/*PublishResult pu= */snsClient.publish(publishRequest);
}

}

下面是我的删除和抓取数据的实现,首先假设mongodb为空:

删除照片实现:

@Override
//deletes photo from mongoDB... but doesn't send out an email stating phootid
public String deletePhoto(String id, String PhotoId){
String mssg="";
if(accountRepo.exists(id)){
UserAccounts userAccounts=accountRepo.findById(id);
UserPhotos photos = photoRepo.findByPhotoId(PhotoId);
mssg="The picture "+photos.getPhotoId()+" has been deleted from the application";
EmailNotifications.deletePic(userAccounts.getEmail(),topic,mssg);
photoRepo.delete(PhotoId);
return "Photo is deleted";
}
else
return "Photo does not exist";
}

第一次从脸上抓照片。用户最多只能收到一条通知,但我一直收到几条消息。

@Override
public UserAccounts create(FacebookClient facebookClient){
User me = facebookClient.fetchObject("me", User.class);
UserAccounts userAccounts = new UserAccounts();
userAccounts.setEmail(me.getEmail());
userAccounts.setGender(me.getGender());
userAccounts.setId(me.getId());
userAccounts.setName(me.getName());
accountRepo.save(userAccounts);
EmailNotifications.SignUP(me.getEmail(), topic);
return userAccounts;
}

有人能在这个上帮我吗

根据您的描述和代码判断,您为用户下载时收到的电子邮件可能是订阅确认电子邮件,因为EmailNotifications.SignUp所做的只是订阅电子邮件地址。

我想,当你删除图片时,你没有收到任何电子邮件的原因是因为你还没有确认订阅。在订阅确认电子邮件中,应该有一个链接,您可以点击以确认订阅。

至于为什么每次下载都会收到电子邮件,我无法从你的代码中判断,但在create方法中,你显示在调用SignUp检查用户是否已经存在时没有if块,我想这是你的问题。

顺便说一句,如果你的应用程序正在与用户交互,并且你想要一个良好的电子邮件体验,你可能会更好地使用SES,它可以让你完全控制电子邮件的格式和品牌。

最新更新