【问题标题】:Is there a way to bring an application to foreground on push notification receive?有没有办法在推送通知接收时将应用程序带到前台?
【发布时间】:2018-07-18 04:07:39
【问题描述】:

例如在 WhatsApp 中。当我打电话给某人时,即使应用程序处于后台或被终止,活动也会打开。我怎样才能实现相同的?此外,我不希望我的应用程序启动新活动,因为该应用程序将在受控环境中使用。我的应用程序将始终在后台。用户将在应用程序打开(后台或前台)时收到通知,因此当他们收到此通知时,他们的应用程序应自动应用程序(而不是启动新活动),而只是将当前活动推送到前台。

为了更好地理解这是我想要实现的: 用户 A 打开应用程序,然后将其最小化。所以现在他有资格接收通知。该应用程序当前在 Activity A 上。当用户收到通知时,我们将收到的数据推送到列表中并在 Activity A 上显示。我要做的就是当用户 A 收到推送通知时,将 Activity A 推送到前台不再创建 Activity A 或任何类似的东西,只需将其置于前台即可。任何帮助将非常感激。谢谢!

【问题讨论】:

    标签: android firebase android-activity push-notification state


    【解决方案1】:

    您可以使用意图标志。

     Intent.FLAG_ACTIVITY_SINGLE_TOP 
    

    你的代码会是这样的

    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);
        }
    }
    

    【讨论】:

    【解决方案2】:

    关注this answer。你可以在你的activity中注册一个广播,或者直接从你的onMessageReceived()调用方法

    【讨论】:

    • 会试一试,让您随时了解情况!
    【解决方案3】:

    收到消息 start NotificationActivity 将检查您的应用程序是否已经运行然后finish 该活动将在堆栈上为您带来最后打开的活动,如果应用程序未运行将启动您的@ 987654325@

        @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();
        }
    }
    

    查看此answer 和此answer 了解更多详情。

    【讨论】:

    • 我不想开始一个新的活动,只是把当前活动带到前台。导致列表显示不会在任何地方持续显示,所以如果我继续启动活动列表显示将毫无意义
    • Start NotificationActivity 只是让应用程序进入前台然后杀死打开的活动将使您回到返回堆栈上的最后一个打开列表的技巧。考虑为您的列表活动处理onSaveInstanceStateonRestoreInstanceState
    猜你喜欢
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2017-05-11
    相关资源
    最近更新 更多