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!
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.
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);
}
}
Follow this answer. You can register a broadcast in your activity, or directly call the method from your onMessageReceived()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With