我有一个 PendingIntent 在点击一个通知时触发,该通知基本上将一个意图发送到我的主菜单(设置为 singleTop 启动模式)。这会触发 onNewIntent,然后加载我想要的数据并使用加载的数据转到另一个活动。不幸的是,如果应用程序当前关闭,onNewIntent 不起作用,所以我不得不从 onCreate 调用 onNewIntent 并在 onNewIntent 中包含一个签入。
一切都很好,除了现在在您触发通知之后,pendingIntent 似乎对每次调用 onCreate 时触发都很满意,它不会消失。我不确定如何在使用后进行检查甚至清除待处理的意图。我的代码如下:
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if(extras != null && extras.containsKey("notification")) {
if(extras.getBoolean("notification", false)) {
loadData(extras.getString("data"));
Intent myIntent = new Intent(this, OtherClass.class);
startActivity(myIntent);
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
//Code snippet of notification setup
notification = new Notification(R.drawable.icon, "Notification Reminder", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, MainMenu.class);
Bundle bundle = new Bundle();
contentTitle = "Notification_Title";
bundle.putBoolean("notification", true);
bundle.putString("data", contentTitle.toString());
notificationIntent.putExtras(bundle);
notificationIntent.setData((Uri.parse("application://"+SystemClock.elapsedRealtime())));
contentTitle = contentTitle + " click me!";
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, notificationText, contentIntent);