我正在使用Parse推送通知,并使用这些方法提醒用户该消息是什么。我如何显示AlertDialog一次,然后从意图中删除该数据,以便在应用程序只是暂停而不是被杀死的情况下它不会再次弹出?
onCreate()
结束时handlePushIntent(getIntent());
我覆盖onNewIntent()
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handlePushIntent(intent);
}
和我的方法:
private void handlePushIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null || extras.isEmpty())
return;
if (extras.containsKey("com.parse.Data")) {
try {
JSONObject data = new JSONObject(extras.get("com.parse.Data")
.toString());
String alert = data.getString("alert");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(alert);
builder.setNeutralButton(android.R.string.ok, null);
builder.show();
} catch (Exception e) {
Log.d("VNL", "Push Error", e);
}
}
}
答案 0 :(得分:1)
IMO事实上,intent框架会自动将intent对象添加到后台堆栈,这会导致你在暂停/恢复时遇到“状态”问题......
还有其他方案可以使用消息循环和处理程序,您可以通过使用处理程序/消息循环在屏幕上显示某些内容(alert / progressmessage),并使用消息和对象(如intent)消息消息后面/堆栈。
'最简单的示例'回答here显示内联处理程序/ runnable,您可以在没有后台/堆栈的情况下发出警报
答案 1 :(得分:0)
我使用另一种方法。为了显示推送通知,我使用EventBus并将EventBus上的通知作为粘性事件发布。一旦我显示消息,我就从EventBus中删除该事件。
有关StickyEvents的更多详细信息,请参阅http://greenrobot.org/eventbus/documentation/configuration/sticky-events/。