Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to bring an application to foreground on push notification receive?

For example in WhatsApp. When I call someone a activity opens up even if the app is in background or killed. How can I implement the same? Also I don't want my application to start a new activity cause this application will be used in a controlled environment. My application will always be in the background. The users will receive notifications when the app is open (background or foreground) so when they receive this notification they app should automatically app (not start a new activity) but just push the current activity to the foreground.

To best understand this is what I am trying to implement: User A opens the app and then minimizes it. So now he is eligible to receive notifications. The application is currently on Activity A. When the user receives the notification we push the data received into a list and show it on Activity A. All I want to do is when User A receives a push notification to just push the Activity A to foreground no creating Activity A again or anything of that sort but just bring it to foreground. Any help would be much appreciated. Thank you!

like image 743
Rahul Thyagaraj Avatar asked Jul 18 '18 04:07

Rahul Thyagaraj


People also ask

How do I show notification when an app is in foreground iOS?

If you'd like to have iOS display your notification while your app is running in the foreground, you'll need to implement the UNUserNotificationCenterDelegate method userNotificationCenter(_:willPresent:withCompletionHandler:) , which is called when a notification is delivered to your app while it's in the foreground.


2 Answers

You can use intent flag.

 Intent.FLAG_ACTIVITY_SINGLE_TOP 

And your code will be like

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        Intent intent= new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }
}
like image 132
Anisuzzaman Babla Avatar answered Nov 07 '22 23:11

Anisuzzaman Babla


Follow this answer. You can register a broadcast in your activity, or directly call the method from your onMessageReceived()

like image 1
Vihaari Varma Avatar answered Nov 07 '22 23:11

Vihaari Varma