1

I've searched the internet and couldn't find any solution to this. Then I've came up with a solution, but I am not sure if it is elegant. I'll post my own solution as well.

Basically what I want to do is to check if application is running when notification is clicked, not when notification comes (I already know how to do the latter). The difference is that, a notification can come when app is running but user can click on it later when app is not running. So, I need to check if app is running when notification is clicked.

How to do that?

NecipAllef
  • 486
  • 1
  • 5
  • 18

2 Answers2

1

Create an activity with no display, call it NotificationActivity. Use this activity for notification click and make the appIsRunning() check in that activity.

For creating an activity with no display, have these in AndroidManifest.xml:

    <activity
            android:name=".NotificationActivity"
            android:theme="@android:style/Theme.NoDisplay"
            android:noHistory="true"
            android:excludeFromRecents="true"/> 

For more info on invisible activity: http://androidblog.reindustries.com/android-cheats-and-tips-invisible-activity/

NecipAllef
  • 486
  • 1
  • 5
  • 18
1

Here is one way of checking the current state of your application. Execute it when user taps on the notification and do whatever you need to do based on state of your app.

Community
  • 1
  • 1
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
  • In the [link](http://stackoverflow.com/questions/5504632/how-can-i-tell-if-android-app-is-running-in-the-foreground/11030799#11030799) it says how to check if app is running, but there is nothing about notification click event. You said "execute it when user taps" but there isn't really a `OnClickListener` for notifications, is there? Only way to implement is to use `PendingIntent` and that's why I've used another activity. – NecipAllef Oct 19 '15 at 09:16