Android的:如何避免点击一个通知要求的onCreate()通知、Android、onCreate

2023-09-12 09:04:54 作者:我们也有很多烦恼

在我的应用程序通知与通知的用户,如果有什么特别的情况发生:

In my application I notify the user with notifications, if something special happens:

public void triggerNotification(String msg) {
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent contentIntent = new Intent(this, ABC.class);
        Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
        notification.setLatestEventInfo(this, "ABC", msg, PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT));
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(notificationCounter, notification);
        notificationCounter++;
}

如果用户点击通知,所述的onCreate()方法被调用。但我想,在我的应用程序的具体方法被调用时,或者如果应用程序是不是在前台,这是带回前台。

If the user clicks on the Notification, the onCreate() method is called. But I want that a specific method in my app is called, or if the app is not in the foreground, that it is brought back to the foreground.

我知道有很多的教程解释如何处理通知,但我就是不明白,他们完全,并没有以往任何时候都能够实现类似的事情,我想。

I know there are lots of tutorials that explain how to handle notifications, but I just don't understand them completely and wasn't ever able to implement the things like I'd like to.

推荐答案

要带上你的应用推到前台,如果它已经在运行,你需要在你的意图设置不同的标志:

To bring your app to the foreground if it is running already you need to set different flags on your intent:

contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

有关运行的具体方法,你可以只随意图和跨preT在你的应用程序来决定运行哪些方法传递额外的信息。

For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.