java内网推送app,java – 使用来自app-engine的Parse发送推送通...

Parse公开了一个RESTful API,您可以使用与您的示例类似的方式(稍微调整).

使用解析进行推送通知时,您希望发送内容的每个用户都由向Parse注册的“安装”对象表示.你可以在这里找到更多信息.可以通过安装REST API对安装执行CRUD操作.

他们有2种方式发送推送:频道和’高级目标’.您应该能够使用“高级目标”来指定deviceTokens(就像您在示例中所做的那样).

创建用户安装:

URL target = new URL("https://api.parse.com/1/installation");

HttpURLConnection connection = (HttpURLConnection) target.openConnection();

connection.setRequestMethod("PUT");

connection.setRequestProperty("Content-Type", "application/json");

connection.setRequestProperty("X-Parse-REST-API-KEY", "${REST_API_KEY}");

connection.setRequestProperty("X-Parse-Application-Id", "${APPLICATION_ID}");

connection.setDoInput(true);

connection.setDoOutput(true);

String installationCreation = "{\"appName\":\"Your App Name\"," +

"\"deviceType\":\"android\",\"deviceToken\":\"" + userDeviceToken + "\"}";

try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {

out.write(installationCreation);

}

connection.connect();

if (connection.getResponseCode() != 201) {

log.error("User Create Failed");

} else {

String response = connection.getResponseMessage();

// response content contains json object with an attribute "objectId"

// which holds the unique user id. You can either use this value or

// deviceToken to send a notification.

}

正如您所料,可以通过向https://api/parse.com/1/installation/ {objectId}发送PUT请求来更新此条目

发送方式大致相同.只需用push api和json替换uri即可

URL target = new URL("https://api.parse.com/1/push");

HttpURLConnection connection = (HttpURLConnection) target.openConnection();

connection.setRequestMethod("POST");

connection.setRequestProperty("Content-Type", "application/json");

connection.setRequestProperty("X-Parse-REST-API-KEY", "${REST_API_KEY}");

connection.setRequestProperty("X-Parse-Application-Id", "${APPLICATION_ID}");

connection.setDoInput(true);

connection.setDoOutput(true);

String notification =

"\"where\": {\"deviceType\": \"android\",\"deviceToken\": { \"$in\" :" + deviceTokens.toString() + "}},"+

"\"data\": {\"alert\": \"A test notification from Parse!\" }";

try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {

out.write(notification);

}

connection.connect();

if (connection.getResponseCode() != 201) {

log.error("Notification Failed");

}

希望这可以帮助

编辑:修复示例拼写错误,现在使用java.net类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值