简体   繁体   English

有没有办法在推送通知接收时将应用程序带到前台?

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

For example in WhatsApp. 例如在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. 为了更好地理解这是我想要实现的:用户A打开应用程序,然后最小化它。 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. 该应用程序当前在活动A上。当用户收到通知时,我们将收到的数据推送到列表中并将其显示在活动A上。我想要做的是当用户A收到推送通知以将活动A推送到前台时不再创建活动A或任何类似的东西,只是将它带到前台。 Any help would be much appreciated. 任何帮助将非常感激。 Thank you! 谢谢!

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() 您可以在活动中注册广播,或直接从onMessageReceived()调用该方法

On message received start NotificationActivity that will check if your application already running then finish that activity will bring you last opened activity on the stack , And if app not running will start your MainActivity 收到消息后,启动NotificationActivity将检查您的应用程序是否已经运行然后finish 该活动将为您带来最后一次打开的活动 ,如果应用程序未运行将启动您的MainActivity

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

     // Check if message contains a data payload.
       if (remoteMessage.getData().size() > 0) {
         Log.d(TAG, "Message data payload: " + remoteMessage.getData());
       }

     // Check if message contains a notification payload.
      if (remoteMessage.getNotification() != null) {
          startActivity(new Intent(this , NotificationActivity.class));
        }  
     }


public class NotificationActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        // If this activity is the root activity of the task, the app is not running
        if (isTaskRoot())
        {
            // Start the app before finishing
            Intent startAppIntent = new Intent(getApplicationContext(), MainActivity.class);
            startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startAppIntent);
        }

      // Now finish, which will drop the user in to the activity that was at the top of the task stack
        finish();
    }
}

Check this answer , and this answer for more details. 查看此 答案 ,以及此答案以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在收到应用程序启动器之类的通知推送后,将Android应用程序置于前台 - Bring the Android app to foreground after recieving a notification push like the application launcher 当推送到达时,将cordova应用程序带到前台 - Bring cordova application to foreground when push arrives 接收GCM推送通知(Cordova)时将应用程序带到前台 - Bring app to foreground when receiving GCM Push notification (Cordova) 工作灯:应用程序在前台时无法接收推送通知 - Worklight: App is not able to receive the push notification when in foreground 将应用程序带到后台然后到前台 - Bring application to background and then to foreground 将Crosswalk应用程序引入前台 - Bring Crosswalk Application in foreground 当应用程序在前台时未收到FCM推送通知,但在应用程序在后台时收到 - FCM Push notification didn't receive when app is in foreground but receive when app in background 通知只有在后台运行时才将Activity带入前台 - Notification to bring Activity into foreground only if it is in background Android 在 Firebase 通知上将应用程序带到前台 - Android bring app to foreground on Firebase notification 推送通知未显示在 Android 前台 - Push notification not showing in Android foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM