我正在执行通知任务。当管理员在管理面板中更改订单状态时,我想执行通知功能。截至目前,它每 30 秒运行一次。我的主要任务是仅在订单状态发生变化时才执行通知功能。
例如订单号001的订单状态为待处理。现在,管理员将此订单状态更改为已接受。然后我想执行这个函数并在我的 Android 应用程序中显示通知。如果订单状态在 30 秒后仍保持不变,或者如果管理员没有更改状态,则不应执行。
这是代码
if (response != null) {
String order_status = response.getString("order_status");
String status_desc = response.getString("status_description");
createNotification(getActivity(), isLoggedIn, order_id, order_status, status_desc); // I want to execute this function only if the order status changes
}
如有帮助,我们将不胜感激
创建订单状态常量
final String PENDING = "pending"
final String ACCEPTED = "accepted"
final String DELIVERED = "delivered"
// Using IF
if (order_status.equalsIgnoreCase(PENDING)){
//execute your function according to your status
}else if(order_status.equalsIgnoreCase(ACCEPTED)){
}else if (order_status.equalsIgnoreCase(DELIVERED)){
}
// using switch case
switch(order_status.toLowerCase()){
case PENDING:
//execute your function according to your status
break;
case ACCEPTED:
break
case DELIVERED:
break;
default:
System.out.print("Add more cases");
}
您必须以某种方式识别状态变化。也许您可以用上次修改时间来装饰状态,并且可以在发送通知之前检查该时间。
让我们回顾一个案例,当管理员将订单状态更改为某个值并立即(30 秒内)撤消其更改时。在这种情况下应该发生什么?显然,没有发送任何通知,因为订单状态没有改变。
让我们考虑另一个情况。如果您的系统出现故障该怎么办?系统启动后用户会收到通知吗? 30 秒(或任何)时间范围不是一个好的决定,因为您的系统可能会停机未知的时间。
为了解决您的任务,我建议采用以下方法。