If the application is closed (not running), then you need to run one activation while clicking on the notification. And if the application is running, then another. How to do it?

    3 answers 3

    And then GCM? This refers to the flags PendingIntent.getActivity () , which are specified in the notification. Depending on this, it is possible to launch or, on the contrary, not to launch a new Activity when clicking on a notification.

      1) Override the Application class

       public class MyApplication extends Application { public static final boolean IS_ACT_RUNNING = false; } 

      2) Indicate this class in the manifest in a special tag.

       <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > ... <application android:name=".MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/ThemeDark" > ... <receiver android:name=".NotifClickBroadcastReceiver " android:exported="false" > <intent-filter> <action android:name="myAction" /> </intent-filter> </receiver> </application> </manifest> 

      3) In this class, create an IS_ACT_RUNNING variable of type boolean

      4) Assign the variable true to the desired activation in the onResume () methods; in onPause () - false;

       @Override protected void onResume() { super.onResume(); MyApplication.IS_ACT_RUNNING=true; } @Override public void onPause() { super.onPause(); MyApplication.IS_ACT_RUNNING=false; } 

      5) When clicking on a notification, check this variable. So you will know if the desired activity is running.

       public class NotifClickBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String isActRunning = String.valueOf( MyApplication.IS_ACT_RUNNING); Log.e("LOG", "isActRunning: "+isActRunning ); } } 

      When creating a notification, specify the call of the above mentioned receiver by clicking on the notification:

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context); Intent i = new Intent(this, NotifClickBroadcastReceiver.class); i.setAction("myAction"); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pi); 
      • Have you ever written standard code examples? Beginners, like me, will not understand how to use your example - zayn1991
      • one
        @ zayn1991, but not a question. - Yuriy SPb
      • Examples reset, so reset = D - zayn1991

      Start somewhere a flag that determines whether the application is running or not.