Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if my app is open when I managed a push-notification

Using Android, when I received a notification push throw my GCMIntentService, I want to know if my app is open or not, because if my app is open when the user click on the notification I want to do nothing, but if the app is close I want to open the app.

like image 918
jlmg5564 Avatar asked May 07 '13 19:05

jlmg5564


People also ask

Do push notifications work when app is open?

App publishers can send them at any time, since users don't have to be in the app or using their devices to receive them. Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app.

How do I know if push notification is working?

Sending a test Android push notificationClick on Settings and open Mobile Apps. Click on the Android App and make sure the Firebase API key has been configured. Click on Test Push and enter the device token for your test device. Add a test payload and send the test.

Do push notifications work when app is closed IOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.


2 Answers

Launch the root activity (the one that has ACTION=MAIN and CATEGORY=LAUNCHER in the manifest) and add Intent.FLAG_ACTIVITY_NEW_TASK. If the app is already active (no matter which activity is on top) this will just bring the task to the front. If the app isn't active, it will start it with your root activity.

like image 155
David Wasser Avatar answered Sep 30 '22 18:09

David Wasser


Define this in all the activities : 1.) Define a static final boolean flag named 'check_running mode' 2.) Define(override) onResume() and onPause() methods in all activities. 3.) Set the value of this falg as 'true' in onResume() and 'false' in OnPause() methods respectively. 4.) Check out when u receive the push notification : a. If falg value is true it means app is in forground so do nothing in that case b. If flag value is false it means app is in background so You can open the app in that case

NOTE: The falg must be static final as you can change it from any of the activity and access it in your receiver class simply. I hope it'll work for you!

1 :
static boolean check_running mode = false;


-------------------
2:
@Override
protected void onResume() {
    super.onResume();

    check_running mode = true;
}

@Override
protected void onPause() {
    check_running mode = false;

    super.onPause();
}

---------------------
3 :
if (check_running mode) {
    showUserView();
}
else {
    showNotification();
}
like image 28
Maddy Avatar answered Sep 30 '22 19:09

Maddy