java - 使用 FCM、SQL、java 向多个设备发送推送通知
问题描述
我有一个向注册设备发送推送通知的工作服务,它运行良好,但我在向多个设备发送推送通知时遇到问题。我正在尝试使用循环,但它没有帮助。我认为计算响应可能会给我结果。也许布尔表达式中的问题,我不猜。也许有人知道在这种情况下该怎么做。感谢您的回复和以下代码:
@RequestMapping(value = "/sendtodeviceid", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> send() throws JSONException, SQLException {
try {
System.out.println("\n" + "send massege to devicesid" + "\n" + "start to get ID" + "\n");
ArrayList <String> deviceList = new ArrayList<>();
Statement statement = jdbcService.getStatement();
String selSql = String.format("SELECT deviceid FROM courier WHERE isActive = true");
ResultSet rs = statement.executeQuery(selSql);
// check equals for more deviceid
while (rs.next()) {
String deviceid = rs.getString("deviceid");
System.out.println("DEVAICE ID which true from sending message " + "\n" + deviceid + "\n");
deviceList.add(deviceid);
if (!deviceid.equals(deviceid)) {
String newdeviceid = deviceid;
deviceList.add(newdeviceid);
}
}
System.out.println(deviceList + "\n");
// find some solution for loop sending message to all device
for (String iddevice: deviceList) {
System.out.println("DEVICE ID: " + iddevice + "\n");
// create jsonObject look like this
// {
// "data":
// {"address":"latitude420&longitude420",
// "click_action":".MessageOrder",
// "order":"#420"},
// "to":
// "d2Hxxa6PNYw",
// "priority":"high"
// },{}
do {
JSONObject body = new JSONObject();
body.put("to", iddevice);
body.put("priority", "high");
// JSONObject notification = new JSONObject();
// notification.put("title", "Wise delivery");
// notification.put("body", "It is personal order!!!");
// notification.put("icon", "main_logo_black");
// notification.put("click_action", ".MessageOrder");
JSONObject data = new JSONObject();
data.put("order", "#421");
data.put("address", "latitude420&longitude421");
data.put("click_action", ".MessageOrder");
// data.put("icon","main_logo_black");
// body.put("notification", notification);
body.put("data", data);
HttpEntity<String> request = new HttpEntity<>(body.toString());
System.out.println("JSON file request" + request);
CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);
CompletableFuture.allOf(pushNotification).join();
try {
String firebaseResponse = pushNotification.get();
return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} while (iddevice == null);
}
} catch (SQLException e) {
System.out.println("don't selectSQL" + "\n" + "SELECT deviceid FROM courier ");
}
return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}
终端显示:
// start
send message to devicesid
start to get ID
// get device id from database
DEVAICE ID which true from sending message
d2Hxxa6PNYw:
DEVAICE ID which true from sending message
eb0s9KRXac8:
// create the ArrayList
[d2Hxxa6PNYw, eb0s9KRXac8]
// and at this step I have a problem
DEVICE ID: d2Hxxa6PNYw
JSON file request<{"data":{"address":"latitude420&longitude421",
"click_action":".MessageOrder",
"order":"#421"},
"to":"d2Hxxa6PNYw",
"priority":"high"},{}>
// after this must be the next step in loop)
解决方案
您可以为用户订阅特定主题,而不是使用循环。然后,您可以使用 Firebase 控制台或编写服务器端逻辑来发送通知。
FirebaseMessaging.getInstance().subscribeToTopic("news")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = getString(R.string.msg_subscribed);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
推荐阅读
- maven - 运行maven时编译问题
- python - 是否有任何清除pycharm输出屏幕的功能?
- text-to-speech - 使用 R 批量生成 text2speech
- c# - 如何在 WinForms、C# 中保持选中的已禁用单选按钮
- angular - 如何使用 Angular 9 和 NG_VALUE_ACCESSOR?
- django - 如何在 Django 中输入当前日期和时间?
- vue.js - 如何在向组件传递不同数据的同时多次使用组件?
- c++ - 用于查找二叉树高度的递归代码
- c# - 从 for 循环中断的用户输入
- ruby-on-rails - 在 Rails API 中存储图像并将其传递给 React App?