Created
May 28, 2014 12:55
-
-
Save uemuraj/4372191cfc04c38e1d97 to your computer and use it in GitHub Desktop.
Amazon SNS Mobile Push を使ってみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.sierra1337.aws.sns; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.BasicAWSCredentials; | |
import com.amazonaws.services.sns.AmazonSNS; | |
import com.amazonaws.services.sns.AmazonSNSClient; | |
import com.amazonaws.services.sns.model.CreatePlatformEndpointRequest; | |
import com.amazonaws.services.sns.model.CreatePlatformEndpointResult; | |
/** | |
* Google Cloud Messaging for Android (GCM) の登録 ID を受け取り、 Amazon SNS Mobile Push | |
* で利用できるように登録するサーブレット。<br> | |
* ここでは GCM のサンプルアプリの実装を受けての記述をしていますが、他のプラットフォーム向けでも処理はほぼ変わらないはずです。 | |
*/ | |
@WebServlet("/gcm/*") | |
public class SNSMobilePushGcmServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
private AWSCredentials awsCredentials; | |
private String snsApiEndpoint; | |
private String applicationArn; | |
@Override | |
public void init() throws ServletException { | |
String accessKey = "**************"; | |
String secretKey = "*********************************"; | |
awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | |
snsApiEndpoint = "https://sns.ap-northeast-1.amazonaws.com"; | |
applicationArn = "arn:aws:sns:ap-northeast-1:999999999999:app/GCM/aaaaaaaaaaa"; | |
} | |
private String getTopicArn() { | |
return "arn:aws:sns:ap-northeast-1:999999999999999:bbbbbbbbbbbb"; | |
} | |
@Override | |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
String pathInfo = req.getPathInfo(); | |
String regId = req.getParameter("regId"); | |
log("regId = " + regId); | |
try { | |
switch (pathInfo) { | |
case "/register": | |
register(regId, req.getRemoteAddr(), getTopicArn()); | |
break; | |
case "/unregister": | |
unregister(regId); | |
break; | |
default: | |
resp.sendError(HttpServletResponse.SC_NOT_FOUND); | |
break; | |
} | |
} catch (Exception e) { | |
throw new ServletException(e); | |
} | |
} | |
private void register(String platformToken, String customData, String topic) | |
throws Exception { | |
AmazonSNS sns = new AmazonSNSClient(awsCredentials); | |
sns.setEndpoint(snsApiEndpoint); | |
CreatePlatformEndpointRequest req = new CreatePlatformEndpointRequest(); | |
req.setPlatformApplicationArn(applicationArn); | |
req.setToken(platformToken); | |
req.setCustomUserData(customData); | |
CreatePlatformEndpointResult res = sns.createPlatformEndpoint(req); | |
sns.subscribe(topic, "application", res.getEndpointArn()); | |
} | |
private void unregister(String regId) throws Exception { | |
// 実装していません。 Management Console から Subscribe と Endpoint を削除してください。 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment