从通知打开给出 Null 异常

Open from notification gives NullException

我正在尝试通过通知推送打开我的应用程序。但我有一个问题,我有 3 个活动,activity 作为启动器,还有 2 个。

当我点击推送时:

new Intent(this, ClientActivity.class) 应用程序在之前运行的 activity 中打开。如果应用程序处于前台或后台,则相同。

问题是当我点击一个通知并且应用程序被终止时,ClientActivity 没有加载,所以应用程序生成 Runtime Exception 因为 ClientActivity 在那里。

我怎么知道 Activity 当前是 运行?

Intent intent = new Intent(this, activityToOpen);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
if(pushTypeSecureSignature){
    intent.putExtra("PUSH_TYPE_SECURE_SIGNATURE", true);
}
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 0);

如果您希望单击 Notification 将您的应用程序以其进入后台时所处的任何状态带到前台,则只需使用此:

Intent = PackageManager.getLaunchIntentForPackage("my.app.package.name")

现在将这个 Intent 包裹在 PendingIntent 中,然后将其放入您的 Notification

这会将您的应用带到前台(如果它是 运行),无论它处于什么状态。如果您的应用不是 运行,这只会启动根 Activity 你的应用程序(当用户点击主屏幕上的应用程序图标时发生同样的事情)。